Socket interface multi-threaded data transfer
Service-side code
Myserver.c
#include <sys/socket.h>#include<sys/types.h>#include<signal.h>#include<string.h>#include<netinet/inch.h>#include<unistd.h>#include<stdlib.h>#include<stdio.h>#include<arpa/inet.h>#definePort_num 5555#defineMax_client 100typedefstruct { intindex; int using;//using or notpthread_t Tid; intclient_com_fd; structsockaddr_in client_addr; intClient_addr_len;} mythread_arg_t;mythread_arg_t Pthread_arg_array[max_client];p rint_thread (intindex) { inti =index; printf ("=======================thread data=======================\n"); printf ("Pthread_arg_array[i].index =%d\n", Pthread_arg_array[i].index); printf ("pthread_arg_array[i].using =%d\n", Pthread_arg_array[i].using); printf ("Pthread_arg_array[i].tid = 0x%lx\n", Pthread_arg_array[i].tid); printf ("pthread_arg_array[i].client_com_fd =%d\n", PTHREAD_ARG_ARRAY[I].CLIENT_COM_FD); printf ("pthread_arg_array[i].client_addr =%s\n", Inet_ntoa (PTHREAD_ARG_ARRAY[I].CLIENT_ADDR.SIN_ADDR)); printf ("=======================thread end========================\n");}void*do_client (void*Arg) { Charbuf[1400]; inti = (int)(Long) Arg; while(1) { intret = recv (PTHREAD_ARG_ARRAY[I].CLIENT_COM_FD, buf,sizeof(BUF),0); if(ret = =-1|| RET = =0) {perror ("recv"); Break; } if(!STRNCMP (BUF,"Exit",4)) { Break; } printf ("recv from client address:%s Data:%s\n", Inet_ntoa (pthread_arg_array[i].client_addr.sin_addr), buf); } print_thread (i); Close (PTHREAD_ARG_ARRAY[I].CLIENT_COM_FD); Pthread_arg_array[i].using=0;}voidClient_exit (intSigno) {pid_t pid; PID=Wait (NULL); //Close (LISTEN_FD);printf"pid =%d exit.\n", PID);}intMainintargcChar**argv) { intLISTEN_FD = socket (af_inet, Sock_stream,0 ); structsockaddr_in myaddr; Myaddr.sin_family=af_inet; Myaddr.sin_port=htons (Port_num); Myaddr.sin_addr.s_addr=htonl (Inaddr_any); intret; inti; Signal (SIGCHLD, client_exit); RET= Bind (LISTEN_FD, (structSOCKADDR *) &myaddr,sizeof(structsockaddr)); if(-1==ret) {Perror ("Bind"); Exit (1); } Listen (LISTEN_FD, -); structsockaddr_in client_addr; intClient_addr_len =sizeof(structsockaddr); intclient_com_fd; while(1) { //find an no using entry for(i =0; (I < max_client) && (1= = Pthread_arg_array[i].using); i++) ; if(I >=max_client) {printf ("overload!\n"); Close (CLIENT_COM_FD); Continue; } printf ("i =%d\n", i); CLIENT_COM_FD= Accept (LISTEN_FD, (structSOCKADDR *) &client_addr, &Client_addr_len); Pthread_arg_array[i].using=1; printf ("IP Address%s has send connect request.\n", Inet_ntoa (CLIENT_ADDR.SIN_ADDR)); Pthread_arg_array[i].index=i; PTHREAD_ARG_ARRAY[I].CLIENT_COM_FD=client_com_fd; Pthread_arg_array[i].client_addr=client_addr; Pthread_arg_array[i].client_addr_len=Client_addr_len; pthread_t*ptid = &(Pthread_arg_array[i].tid); if(-1= = Pthread_create (Ptid, NULL, Do_client, (void*)(Long) {printf ("pthread_creat () \ n"); Continue; }/*pid_t pid; PID = fork (); if (PID < 0) {perror ("fork"); Exit (3); } else if (PID = = 0) {close (LISTEN_FD); Do_client (CLIENT_COM_FD, client_addr); Close (CLIENT_COM_FD); Exit (0); }*/ } //Close (CLIENT_COM_FD); //Close (LISTEN_FD);}
Client code
Myclient.c
#include <sys/socket.h>#include<netinet/inch.h>#include<stdlib.h>#include<unistd.h>#include<string.h>#include<sys/types.h>#include<stdio.h>#definePort_num 5555#defineLENGTH 1460intMainintargcChar*argv[]) { if(2!=argc) {Exit (1); } intSOCK_FD = socket (af_inet, Sock_stream,0); structsockaddr_in server_addr; Server_addr.sin_family=af_inet; Server_addr.sin_port=htons (Port_num); Server_addr.sin_addr.s_addr= Inet_addr (argv[1]); intret = Connect (SOCK_FD, (structSOCKADDR *) &server_addr,sizeof(structsockaddr)); if(-1==ret) {Perror ("Connect"); Exit (2); } CharBuf[length]; while(1) {printf ("Input str:"); //gets (BUF);fgets (buf, LENGTH, stdin); Send (SOCK_FD, buf, strlen (BUF)+1,0); if(!STRNCMP (BUF,"Exit",4)) { Break; }} close (SOCK_FD);}
Compile link execution: Start the server first, and then start the client on the other terminal. The results of the implementation are as follows:
Socket interface multi-threaded data transfer