Write your own client program
Also is to achieve the function of the previous article, the client program is no longer using Telnet, their own code implementation, the server-side program needs to be modified under
- Server programs
#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <string.h>#include <errno.h>#include <sys/socket.h>#include <arpa/inet.h>voidErrorChar*MSG) {fprintf(stderr,"%s:%s\n", MSG, strerror (errno));Exit(1);}intOpen_listener_socket (void){ints = socket (pf_inet, Sock_stream,0);//Create server socket, IPV4 protocol, connection-oriented communication, TCP protocol if(s = =-1) Error ("Scoket Open Error");returns;}voidBind_to_port (intSocketintPort) {structsockaddr_in name;//server network address structurename.sin_family = pf_inet; Name.sin_port = (in_port_t) htons (port);//server port numberNAME.SIN_ADDR.S_ADDR = htonl (Inaddr_any);//server address, allow connection to all local connections //Set socket to re-use already bound interface intReuse =1;if(socket, Sol_socket, SO_REUSEADDR, (setsockopt) (Char*) &reuse,sizeof(int)) == -1) Error ("Cant set the reuse option on the Scoket");//Bind the server socket to the network address intc = bind (socket, (structsockaddr*) &name,sizeof(name));if(c = =-1) Error ("cant bind to socket");}intSent_msg_to_client (intSocketChar*s) {intresult = Send (socket, S,strlen(s),0);if(Result = =-1)fprintf(stderr,"%s:%s\n","Error talking to client", Strerror (errno));returnResult;}intMainintargcChar Const*argv[]) {intSERVER_SOCKFD;intLen SERVER_SOCKFD = Open_listener_socket (); Bind_to_port (SERVER_SOCKFD,30000);//Listen for connection requests, listening queue Length is ten if(Listen (SERVER_SOCKFD,Ten) == -1) Error ("Cant listen");structSockaddr_storage client_addr;//client_addr to save the connection client details unsigned intAddress_size =sizeof(CLIENT_ADDR);puts("Waiting for Connection");Charbuf[255];//Wait for client connection request to arrive intCLIENT_SOCKFD = Accept (SERVER_SOCKFD, (structSOCKADDR *) &client_addr, &address_size);if(CLIENT_SOCKFD = =-1) Error ("Accept Error"); Sent_msg_to_client (CLIENT_SOCKFD,"Welcome to my Server\n>"); while(len = recv (CLIENT_SOCKFD, buf,sizeof(BUF),0)) >0) {Buf[len] =' + ';printf("%s\n", buf);if(STRNCASECMP ("Who is there?", BUF, A)) Sent_msg_to_client (CLIENT_SOCKFD,"You should say ' Who's is there? '!\r\n>");Else{Sent_msg_to_client (CLIENT_SOCKFD,"Oscar\r\n>"); while(len = recv (CLIENT_SOCKFD, buf,sizeof(BUF),0)) >0) {Buf[len] =' + ';printf("%s\n", buf);if(STRNCASECMP ("Oscar who?", BUF,9) {sent_msg_to_client (CLIENT_SOCKFD,"You should say ' Oscar The Who? '!\r\n> ');Continue; }Else{Sent_msg_to_client (CLIENT_SOCKFD,"Oscar silly question, you get a silly answer\r\n"); Break; } } } }return 0;}
- Client programs
#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <string.h>#include <errno.h>#include <sys/socket.h>#include <arpa/inet.h>voidErrorChar*MSG) {fprintf(stderr,"%s:%s\n", MSG, strerror (errno));Exit(1);}intOpen_listener_socket (void){ints = socket (af_inet, Sock_stream,0);//Create server socket, IPV4 protocol, connection-oriented communication, TCP protocol if(s = =-1) Error ("Scoket Open Error");returns;}intMainintargcChar*argv[]) {intCLIENT_SOCKFD;Charbuf[255];intLenstructSockaddr_in client_addr;//Client network address structureClient_addr.sin_family=pf_inet;//Set as IP communicationCLIENT_ADDR.SIN_ADDR.S_ADDR=INET_ADDR ("127.0.0.1");//server IP addressClient_addr.sin_port=htons (30000);//server port number / * Create a client socket--IPV4 protocol, connection-oriented communication, TCP protocol * /Client_sockfd= Open_listener_socket ();/ * Bind the socket to the client's network address * / if(Connect (CLIENT_SOCKFD,structSOCKADDR *) &client_addr,sizeof(structSOCKADDR)) = =-1) {Error ("Cant connected to server"); }puts("connected to server\n"); LEN=RECV (Client_sockfd,buf,bufsiz,0);//Receive server-side informationbuf[len]=' + ';printf('%s ', buf);//Print server-side information while(1) {//scanf ("%s", buf);Gets (BUF);if(!strcmp(BUF,"Quit")) Break; Len=send (Client_sockfd,buf,strlen(BUF),0); LEN=RECV (Client_sockfd,buf,bufsiz,0); buf[len]=' + ';printf('%s ', buf); } close (CLIENT_SOCKFD);//Close socket return 0;}
Socket communication under Linux (next)