//Network Programming Client#include <stdio.h>#include<stdlib.h>#include<string.h>#include<unistd.h>#include<errno.h>#include<sys/types.h>#include<sys/socket.h>#include<arpa/inet.h>//htons () function header file#include <netinet/inch.h>//inet_addr () header fileintMainintArgChar*args[]) { intSt=socket (Af_inet,sock_stream,0);//Initialize Socket if(st==-1) {printf ("init socket failed! Error message:%s\n", Strerror (errno)); return-1; } structSockaddr_in addr;//defining the IP address structurememset (&ADDR,0,sizeof(addr)); Addr.sin_family=af_inet;//set structure address type to TCP/IP address /*byte Conversion Network transfer data is in bytes, the short type can not be transferred in the network, must be converted to a network type, such as the short type has 2 bytes, in the network transmission can not know which is the high byte, that is the low byte, so The system provides the htons () function to handle. Therefore, to transfer non-char data in the network, the appropriate system functions must be called to convert this type into a char type in the network. */Addr.sin_port=htons (8080);//Specify the port number htons: Convert the short type from the host byte type to the net byte type /*The IP address is an integer in memory, but we are used to writing a string inet_addr () function to convert the IP address of a string into an int type number but inet_addr () has a problem with multiple threads, we generally use the custom type that */addr.sin_addr.s_addr=INET_ADDR ("127.0.0.1"); //call the Connect () function connection structure addr The specified IP address and port number /*Connect () function The second parameter is detailed: The struct sockaddr type is an older type and is now generally used with struct sockaddr_in type but struct sockaddr and Struc T-sockaddr_in types are mutually compatible*/ if(Connect (ST,structSOCKADDR *) &addr,sizeof(addr)) ==-1) {printf ("Connect failed! Error message:%s\n", Strerror (errno)); GotoEND; } Charbuf[1024x768]={0}; strcpy (BUF,"fly on air!\n"); if(Send (St,buf,strlen (BUF),0)==-1)//sending data from a BUF{printf ("send failed! Error message:%s\n", Strerror (errno)); GotoEND; }end:close (ST); return 0;}
//Network programming service side#include <stdio.h>#include<stdlib.h>#include<string.h>#include<unistd.h>#include<errno.h>#include<sys/types.h>#include<sys/socket.h>#include<arpa/inet.h>//htons () function header file#include <netinet/inch.h>//inet_addr () header fileintMainintArgChar*args[]) { intSt=socket (Af_inet,sock_stream,0);//Initialize Socket if(st==-1) {printf ("init socket failed! Error message:%s\n", Strerror (errno)); return-1; } inton=1; if(SetSockOpt (St,sol_socket,so_reuseaddr,&on,sizeof(ON)) ==-1) {printf ("setsockopt failed! Error message:%s\n", Strerror (errno)); GotoEND; } structsockaddr_in addr; memset (&ADDR,0,sizeof(addr)); Addr.sin_family=af_inet; //Server -side binding port number, you need to go to the system settings to open the port numberAddr.sin_port=htons (8080); /*the htonl () function converts a 4-byte long type data into a network type*/addr.sin_addr.s_addr=htonl (Inaddr_any);//Inaddr_any represents all the addresses on this server//bind the IP address to the server if(Bind (St, (structSOCKADDR *) &addr,sizeof(addr)) ==-1) {printf ("bind IP addr failed! Error message:%s\n", Strerror (errno)); GotoEND; } //Server starts Listen if(Listen (St,Ten)==-1) {printf ("Listen failed! Error message:%s\n", Strerror (errno)); GotoEND; } intclient_st=0;//client-side socket structSockaddr_in clientaddr;//IP address of client side Charbuf[1024x768]={0}; intI=0; while(i<1) {memset (&CLIENTADDR,0,sizeof(CLIENTADDR)); socklen_t Len=0;//maximum number of bytes representing the client address /*Accept will block the current thread until a client connects, and the Accept () function returns the socket descriptor on the client side*/Client_st=accept (St, (structSOCKADDR *) &clientaddr,&Len); if(client_st==-1) {printf ("Accept failed! Error message:%s\n", Strerror (errno)); GotoEND; } if(Recv (Client_st,buf,sizeof(BUF),0)==-1)//receiving messages from the client side{printf ("recv failed, send from IP error message:%s\n", Strerror (errno)); Close (Client_st);//close the client-side socket GotoEND; } printf ("%s", BUF); Close (Client_st);//close the client-side socketmemset (BUF,0,sizeof(BUF)); I++; }end:close (ST);//shutting down the server socket return 0;}
. Suffixes:.c. OCC=gccSRCS1=MYCLIENT.CSRCS2=server.cobjs1=$ (srcs1:.c=. O) OBJS2=$ (srcs2:.c=. O) EXEC1=mclientEXEC2=mserverstart:$ (OBJS1) $ (OBJS2) $ (CC)-o $ (EXEC1) $ (OBJS1) $ (CC)-o $ (EXEC2) $ (OBJS2) @echo"-------OK-----------". C.O: $ (CC)-WALL-G-o [email protected]-C $<Clean:rm-F $ (OBJS1) RM-F $ (EXEC1) RM-F $ (OBJS2) RM-F $ (EXEC2)
Linux Network Programming III (socket code details)