#include <Winsock2.h>#include<stdio.h>voidMain () {//The following sentences are fixed.WORD wversionrequested; Wsadata Wsadata; interr; Wversionrequested= Makeword (1,1 ); Err= WSAStartup (wversionrequested, &wsadata);//The function is to load a winsocket library version if(Err! =0 ) { return; } if(Lobyte (wsadata.wversion)! =1||hibyte (wsadata.wversion)!=1) {wsacleanup (); return; } //true Socket Programming SectionSOCKET Socksrv=socket (Af_inet,sock_stream,0);//connectivity-oriented reliability Services Sock_stramsockaddr_in addrsrv;//where local address information is storedAddrsrv.sin_addr. S_un. S_addr=htonl (Inaddr_any);//Htol Converting host byte-order long to network byte-orderaddrsrv.sin_family=af_inet; Addrsrv.sin_port=htons (6001);//The Htos is used to convert the port into characters, and more than 1024 of the digitsbind (Socksrv, (sockaddr*) &addrsrv,sizeof(SOCKADDR));//bind the socket to the appropriate address and portListen (Socksrv, -);//the maximum length in the wait queue is 5sockaddr_in addrclient; intlen=sizeof(SOCKADDR); while(1) {SOCKET sockconn=accept (Socksrv, (sockaddr*) &addrclient,&len);//create a new socket for communication, not the previous listener socket Charsendbuf[ -]; sprintf (SendBuf,"Server IP is:%s", Inet_ntoa (ADDRCLIENT.SIN_ADDR));//The Inet_nota function converts a character to an IP address Charrecvbuf[ -]; Recv (Sockconn,recvbuf, -,0);//server accepts data from clientprintf"%s\n", RECVBUF); if(strcmp (Recvbuf,"Reconnect")==0) {Sleep ( the); strcpy (SendBuf,"finished"); Send (Sockconn,sendbuf,strlen (SENDBUF)+1,0);//the server sends data to the client} closesocket (sockconn);//Close Socket }}
Mac side
#include <netinet/inch.h>//For sockaddr_in#include <sys/types.h>//For socket#include <sys/socket.h>//For socket#include <stdio.h>//For printf#include <stdlib.h>//For exit#include <string.h>//For bzero/*#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h>*/#defineHello_world_server_port 6001#defineBuffer_size 1024#defineFile_name_max_size 512intMainintargcChar**argv) { //set a socket address structure client_addr, which represents the client Internet address, Port structsockaddr_in client_addr; Bzero (&CLIENT_ADDR,sizeof(CLIENT_ADDR));//set the contents of a memory area to 0client_addr.sin_family = af_inet;//Internet Protocol FamilyCLIENT_ADDR.SIN_ADDR.S_ADDR = htons (Inaddr_any);//Inaddr_any means to get the native address automaticallyClient_addr.sin_port = htons (0);//0 means that the system is automatically assigned an idle port//Create a Stream protocol (TCP) socket for the Internet, using Client_socket to represent the client socket intClient_socket = socket (Af_inet,sock_stream,0); if(Client_socket <0) {printf ("Create Socket failed!\n"); Exit (1); } //connect the client socket to the client's socket address structure . if(Bind (Client_socket, (structsockaddr*) &client_addr,sizeof(CLIENT_ADDR))) {printf ("Client Bind Port failed!\n"); Exit (1); } //set a socket address structure server_addr, which represents the server's Internet address, Port structsockaddr_in server_addr; Bzero (&SERVER_ADDR,sizeof(SERVER_ADDR)); Server_addr.sin_family=af_inet; if(Inet_aton ("192.168.1.149", &server_addr.sin_addr) = =0)//the IP address of the server comes from the parameters of the program{printf ("Server IP Address error!\n"); Exit (1); } server_addr.sin_port=htons (Hello_world_server_port); socklen_t Server_addr_length=sizeof(SERVER_ADDR); //initiate a connection to the server, Client_socket represents a socket connection for the client and the server after the connection is successful if(Connect (Client_socket,structsockaddr*) &server_addr, server_addr_length) <0) {printf ("Can not Connect to%s!\n", argv[1]); Exit (1); } CharBuffer[buffer_size]; Bzero (buffer,buffer_size); strcpy (Buffer,"Reconnect"); //send data in buffer to the serverSend (Client_socket,buffer,buffer_size,0); //receiving data from the server into bufferbzero (buffer,buffer_size); intLength =0; if(Recv (Client_socket,buffer,buffer_size,0) >0) {printf ("Received%s\n", buffer); } //Close SocketClose (client_socket); return 0;}
Windows and Mac socket communication