Home > Software engineering >  Modify the following code how to implement a server and multiple clients communication, a great god
Modify the following code how to implement a server and multiple clients communication, a great god

Time:09-26

//server
# include & lt; stdio.h>
# include & lt; Windows. H>
# pragma comment (lib, "Ws2_32. Lib")
/* * * * * defined port/
# define PORT 8888
/* * * * */quantities of data definition biggest
# define MAXDATA 100
/* * * * in the entry function input host IP address * * * */
Int main (int arg c, char * * argv)
{
/* define flag, when the first client, received not receive other client */
Int flag=1;
/* get word */socket nested
The SOCKET server.
Send messages defined/* */
the length of theInt sdMsgLen;
/* start Windows socket service */
WSADATA wsaData;//the Windows sockets DLL version information
WSAStartup (0 x101, & amp; WsaData);
Char * CloseClient="bye";
Char * CloseC_S="byebye";

/* */define the receive data length
Int the receive;
int len;
/* create a server */
Struct sockaddr_in serveraddr;
/* create client */
Struct sockaddr_in addrClient;
/* will serveraddr zeros */
Memset ((void *) & amp; Serveraddr, 0, sizeof (serveraddr));
/* define family agreement */
Serveraddr. Sin_family=AF_INET;
/* define the host address */
Serveraddr. Sin_addr. S_addr=inet_addr (argv [1]).//)." 192.168.1.100
"/* define the host port */
Serveraddr. Sin_port=htons (PORT);
/* get word */socket nested
Server=socket (AF_INET SOCK_STREAM, 0).
/* returns a value of 1, did not start Windows socket service */
If (server==1)
{
Perror (" socket error!" );
}
/* binding successfully returns 0 */
If (bind (server, (struct sockaddr *) & amp; Serveraddr, sizeof (serveraddr))==1)
{
Perror (" cannot be binding!" );
exit(1);
}
Define the client nested word/* */
The SOCKET client;
/* enter monitor client connection and monitored the client sends the message */
While (1)
{
/* definition and initialize the receiving data storage */
Char receiveBuffer [MAXDATA]="\ n";
/* defined and initialized to send data storage */
Char sendBuffer [MAXDATA]="\ n";
Using asynchronous messaging *//*
Bool sendFlag=false;
/* monitor client */
If (listen (server, 20)==1)
{
Perror (" cannot be listening!" );
exit(1);
}
/* received after the first user will lock it, not to receive other users */
If (flag)
{
Len=sizeof (struct sockaddr_in);
/* get word */client nested
The client=accept (server, (SOCKADDR *) & amp; AddrClient, & amp; Len);
If (client==1)
{
Perror (" accept the error ");
continue;
}
New connections: printf (" % s \ n ", inet_ntoa (addrClient. Sin_addr));
To create a message/* */
Char * MSG="Hello, Connected Successfully! \n";
Int len=strlen (MSG);
/* get the send the return value of the length of the return to send a message. If sending failure criterion return 1 */
Int sendLen=send (client, MSG, len, 0);
If (sendLen==1)
{
Perror (" the message is sent failure! \n");
Closesocket (the client);
}
MSG=0;
/* to receive the first client, the lock */
flag=0;
}
/* get the length of the message is received */
The receive=recv (client, receiveBuffer MAXDATA, 0).
/* if the message length to 1, the receiving data failure */
If (the receive==1)
{
Perror (" receive error! \n");
/* close client */
Closesocket (the client);
}

Printf (" % s \ n ", receiveBuffer);
/* test the client sends the end of the call sign */
If (STRCMP (receiveBuffer, CloseClient)==0)
{
Printf (" lose client: % s \ n ", inet_ntoa (addrClient. Sin_addr));
Flag=1;

Closesocket (the client);

}
Else if (STRCMP (receiveBuffer CloseC_S)==0)
{

Closesocket (server);
exit(1);
}
The else
{
Printf (" the Client send message % s: % s \ n ", inet_ntoa (addrClient. Sin_addr), receiveBuffer);
/* when message is received, the server can send messages */
SendFlag=true;
}
While (sendFlag)
{
Printf (" Your Message: ");
/* get server input string */
The scanf (" % s ", sendBuffer);
SdMsgLen=strlen (sendBuffer);
/* get server sends the message length */
Int sendLen=send (client, sendBuffer, sdMsgLen, 0);
If (sendLen==1)
{
Perror (" message sending failed!" );
Closesocket (the client);
}
/* the end of the server sends the message mode, instead of receiving mode */
SendFlag=false;
}
}

return 0;
}









//client
# include & lt; Windows. H>
# include & lt; stdio.h>
# include & lt; Errno. H>
# pragma comment (lib, "Ws2_32. Lib")
Definition and the host connection port/* */
# define PORT 8888
Maximum transmission data volume *//* define
# define MAXDATA 100
/* by the entrance to the function to get the IP address of the host to connect */
Int main (int ars, char * * arg)
{
/* open Windows socket */
WSADATA wsaData;
WSAStartup (0 x101, & amp; WsaData);

/* * define server/
Struct sockaddr_in serveraddr;
/* will serveraddr reset */
Memset ((void *) & amp; Serveraddr, 0, sizeof (serveraddr));
/* define family agreement */
Serveraddr. Sin_family=AF_INET;
Get host IP address/* */
Serveraddr. Sin_addr. S_addr=inet_addr (arg [1]).//);//) "192.168.1.100";
/* * get port/
Serveraddr. Sin_port=htons (PORT);
/* get word */socket nested
The SOCKET s=SOCKET (AF_INET, SOCK_STREAM, 0);
/*
TCP connection to *
* parameter 1: socket returns descriptor
* parameter 2: host IP address and port number pointer
3: * parameter remote geological excuse length
*/
If (connect (s, (struct sockaddr *) & amp; Serveraddr, sizeof (serveraddr))==1)
{
Perror (" connection error!" );
exit(1);
}
/* into the monitor server sent the message, take the asynchronous communication */
While (1)
{
/* initializes the receiving and sending data storage */
Char receiveBuffer [MAXDATA]="\ n";
Char sendBuffer [MAXDATA]="\ n";
/* set delivery mode */
Bool sendFlag=false;
/* if the receive data bits - 1 is wrong, right is the length of the recv returns to receive data */
If (recv (s, receiveBuffer MAXDATA, 0)==1)
{
Perror (" receiving data error!" );
exit(1);
}

Printf (" send message server % s: % s \ n ", inet_ntoa (serveraddr. Sin_addr), receiveBuffer);
/* delivery mode */
SendFlag=true; nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related