functions: divided into clients and servers, the client can transfer strings to the server and transfer files.
Environment:UbuntuCompile method:server.c: gcc –o server server.c Client.c:gcc-o Client Client.cHow to run:Open two terminals, run at one terminal first./server, then run at another terminal./client. Source:server.c
#include <stdio.h>#include<unistd.h>#include<stdlib.h>#include<string.h>#include<sys/socket.h>#include<sys/stat.h>#include<arpa/inet.h>#defineMAXBUF 256#definePORT 7787intMain () {intClen; intSsock,csock; structSockaddr_in client_addr,server_addr;//client, server address Charbuf[maxbuf+5], name[maxbuf+5];//two buffersFILE*FP; if((Ssock=socket (PF_INET,SOCK_STREAM,IPPROTO_TCP)) <0) /*Af_unix indicates that only the single UNIX system can be used for interprocess communication, SOCK_STREAM indicates that the TCP protocol returns a file descriptor when it succeeds, and returns 1 on Failure*/{perror ("Socket Error:"); Exit (1); } Clen=sizeof(CLIENT_ADDR); memset (&SERVER_ADDR,0,sizeof(SERVER_ADDR)); /*set the former sizeof (SERVER_ADDR) byte of the opened server_addr to 0*/server_addr.sin_family=af_inet; /*sin_family refers to the protocol family, in socket programming can only be af_inet*/server_addr.sin_addr.s_addr=htonl (Inaddr_any); /*Inaddr_any means you can communicate with any host.*/Server_addr.sin_port=htons (PORT);/*Sin_port Storage Port number*/ if(Bind (Ssock, (structSOCKADDR *) &server_addr,sizeof(SERVER_ADDR)) <0){ /*bind port with bind: Bind binds the local port with the file descriptor returned by the socket. Success is return 0, failure returns-1*/perror ("bind error"); Exit (1); } if(Listen (Ssock,8) <0){ /*Listen (file descriptor after bind, maximum request queue Length) The Listen function changes the file descriptor of bind to a listening socket, returns 0 successfully, otherwise returns 1*/perror ("Listen error:"); Exit (1); } Csock=accept (Ssock, (structSOCKADDR *) &client_addr, (socklen_t*) &Clen); /*Accept (int sockfd, struct sockaddr *addr,int *addrlen) Addr,addrlen is used to fill the client's program, the server side as long as the delivery of pointers can be successful when the accept return Back to the last server-side file descriptor, this time the server side can write to the descriptor of the message failed to return 1*/ while(1){ if(Recv (Csock,buf, Maxbuf,msg_waitall) >0) //Msg_waitall does not return until the specified number of bytes has been read { if(strcmp (BUF,"@#$%")==0){//means to pass a filerecv (csock,buf,maxbuf,msg_waitall);//Receive file namesprintf (name," %", buf);//format buf as a long string, assigned to name if(Freopen (Name,"W", stdout))!=null) {//open a file in write mode while(recv (Csock,buf,maxbuf,msg_waitall) >0)//Receive file Contents { if(strcmp (BUF,"@#$%")==0) Break;//Roger, @#$% said it was delivered .fwrite (buf,sizeof(Char), strlen (BUF), stdout);//Write File} fclose (stdout); Freopen ("/dev/tty","W", stdout);//puts ("File received success!"); } } Else if(strcmp (BUF,"%$#@")==0){//represents a pass -through characterrecv (csock,buf,maxbuf,msg_waitall);//receive the character that cameprintf ("%s\n", BUF); Send (Csock,"Server Receive Message success!", Maxbuf,0);//Return Confirmation Information}}} close (Csock); return 0;}
client.c
#include <stdio.h>#include<unistd.h>#include<stdlib.h>#include<sys/socket.h>#include<sys/stat.h>#include<arpa/inet.h>#include<string.h>#defineMAXBUF 256#definePORT 7787intMain () {intSsock; intClen, ch; Charbuf[maxbuf+5];//buffers structsockaddr_in server_addr; if((Ssock=socket (PF_INET,SOCK_STREAM,IPPROTO_TCP)) <0){ //setting up sockets, using the Internet, TCP stream transmissionperror ("Socket Error:"); Exit (1); } Clen=sizeof(SERVER_ADDR); memset (&SERVER_ADDR,0,sizeof(SERVER_ADDR)); Server_addr.sin_family=af_inet; Server_addr.sin_addr.s_addr=INET_ADDR ("127.0.0.1"); //Inet_addr:ip converted to 4-byte shaping, which needs to be changed according to the server IPServer_addr.sin_port=htons (PORT); if(Connect (Ssock,structSOCKADDR *) &server_addr,clen) <0){ /*Client Connection server, the parameters are socket file descriptor, address information, address structure size, the Connect function is used by the client to connect with the service side. Return 0 on success, fail back-1*/perror ("Connect Error:"); Exit (1); } while(1) {printf ("1:send message\n2:send file\n3:exit [1/2/3]?"); scanf ("%d",&ch); if(ch==1){//Send Messagescanf ("%s", BUF); Send (Ssock,"%$#@", Maxbuf,0);//tell the server to pass a stringSend (Ssock, buf, Maxbuf,0);//send a character to passrecv (Ssock, buf, maxbuf,msg_waitall);//Receive confirmation Informationputs (BUF); } Else if(ch==2){//Send Fileprintf ("input the filename:"); scanf ("%s", BUF); if(Freopen (BUF,"R", stdin)! =NULL) {Send (Ssock,"@#$%", Maxbuf,0);//tell the server to pass the fileSend (Ssock,buf,maxbuf,0);//Pass file name intCnt=0; while((Ch=getchar ())! =EOF) {buf[cnt++]=ch;//buf buffer size is maxbuf, that is, every read maxbuf characters, sent once if(cnt==maxbuf) {buf[cnt]=0, cnt=0; Send (Ssock,buf,maxbuf,0); } } if(CNT) {buf[cnt]=0, cnt=0; Send (Ssock,buf,maxbuf,0);}//transfer the last part of the MaxbufSend (Ssock,"@#$%", Maxbuf,0);//indicates that the transfer is completefclose (stdin); Freopen ("/dev/tty","R", stdin); } } Else Break; } close (Ssock); return 0;}
Linux-socket Programming (1)