In my spare time, I wrote a simple multi-person chat program using multi-thread and network knowledge in linux, it is intended to explain the steps for writing network programs with gnu c, the issues needing attention, and the usage of linux multithreading.
Compile:
Gcc-Wall-o server-lpthread server. c
Gcc-Wall-o client-lpthread client. c
Server: C code
# Include <sys/types. h>
# Include <sys/socket. h>
# Include <sys/wait. h>
# Include <stdio. h>
# Include <stdlib. h>
# Include <errno. h>
# Include <string. h>
# Include <sys/un. h>
# Include <sys/time. h>
# Include <sys/ioctl. h>
# Include <unistd. h>
# Include <netinet/in. h>
# Include <pthread. h>
// Define the number of simultaneous chats
# Define COUNT 5
// Save the socket
Int socket_fd [COUNT];
// Thread entry function
Void pthread_function (int client_fd ){
Char message [1500];
Char buf [1024];
Int I, recvbytes;
Char name [20];
// Accept and save the client name during the first connection
Recvbytes = recv (client_fd, name, 20, 0 );
Name [recvbytes] = :;
Name [recvbytes + 1] =;
While (1 ){
If (recvbytes = recv (client_fd, buf, 1024, 0) =-1 ){
Perror ("recv error ");
Exit (1 );
}
If (recvbytes = 0 ){
Printf ("% sbye! ", Name );
Break;
}
Buf [recvbytes] =;
For (I = 0; I <COUNT; I ++ ){
If (socket_fd [I] =-1 ){
Continue;
} Else {
Message [0] =;
Strcat (message, name );
Strcat (message, buf );
If (send (socket_fd [I], message, strlen (message), 0) =-1 ){
Perror ("send error ");
Exit (1 );
}
}
}
}
// Close the socket when disconnected and set the descriptor value to-1
Close (client_fd );
For (I = 0; I <COUNT; I ++ ){
If (socket_fd [I] = client_fd ){
Socket_fd [I] =-1;
}
}
// Exit the thread
Pthread_exit (NULL );
}
Int main (){
// Initialize the socket Array
Int I;
For (I = 0; I <COUNT; I ++ ){
Socket_fd [I] =-1;
}
Pthread_t id;
Int sockfd, client_fd;
Socklen_t sin_size;
Struct sockaddr_in my_addr;
Struct sockaddr_in remote_addr;
If (sockfd = socket (AF_INET, SOCK_STREAM, 0) =-1 ){
Perror ("socket ");
Exit (1 );
}
// Configuration information
My_addr.sin_family = AF_INET;
My_addr.sin_port = htons (12345 );
My_addr.sin_addr.s_addr = INADDR_ANY;
Bzero (& (my_addr.sin_zero), 8 );
// Bind
If (bind (sockfd, (struct sockaddr *) & my_addr, sizeof (struct sockaddr) =-1 ){
Perror ("bind ");
Exit (1 );
}
// Listen
If (listen (sockfd, 10) =-1 ){
Perror ("listen ");
Exit (1 );
}
I = 0;
While (1 ){
Sin_size = sizeof (struct sockaddr_in );
If (client_fd = accept (sockfd, (struct sockaddr *) & remote_addr, & sin_size) =-1 ){
Perror ("accept ");
Exit (1 );
}
// Locate an available socket location
While (socket_fd [I]! =-1)
I = (I + 1) % COUNT;
// Save the socket and start the thread for processing
Socket_fd [I] = client_fd;
Pthread_create (& id, NULL, (void *) pthread_function, (int *) client_fd );
}
}
# Include <sys/types. h>
# Include <sys/socket. h>
# Include <sys/wait. h>
# Include <stdio. h>
# Include <stdlib. h>
# Include <errno. h>
# Include <string. h>
# Include <sys/un. h>
# Include <sys/time. h>
# Include <sys/ioctl. h>
# Include <unis