Home > other >  Unxi under simple socket communication, server send function is always wrong, and under the great go
Unxi under simple socket communication, server send function is always wrong, and under the great go

Time:10-24

Server:
# include
# include
# include
# include
# include
# include

# define BUF_SIZE 1024
Void error_handling (char * message);

Int main (int arg c, char * argv [])
{
Int clnt_sock;
Int serv_sock;
Struct sockaddr_in serv_addr clnt_addr;
Char recvBuf [BUF_SIZE];
Char sendBuf [BUF_SIZE];
Socklen_t clnt_addr_sz;
Int recv_size;
Int send_size;
If (arg c!
=2){
Printf (" the Usage:./% s & lt; PORT>" , argv [0]);
The exit (1);
}

If ((serv_sock=socket (PF_INET SOCK_STREAM, 0))==1)
Error_handling (" socket () error ");
Printf (" the Success to establish a socket \ n ");

Memset (& amp; Serv_addr, 0, sizeof (serv_addr));
Serv_addr. Sin_family=AF_INET;
Serv_addr. The sin_addr. S_addr=htonl (INADDR_ANY);
Serv_addr. Sin_port=htons (atoi (argv [1]));
If (bind (serv_sock, (struct sockaddr *) & amp; Serv_addr, sizeof (serv_addr))==1)
Error_handling (" bind () error!" );
Printf (" Success to bind \ n ");

If (listen (serv_sock, 5)==1)
Error_handling (" liten () error ");
Printf (" Success to listen \ n ");

Clnt_addr_sz=sizeof (clnt_addr);
If (clnt_sock=accept (serv_sock, (struct sockaddr *) & amp; Clnt_addr, & amp; Clnt_addr_sz)==1)
Error_handling (" the accept () error!" );
Printf (" % s joins in! . \ n ", inet_ntoa (clnt_addr sin_addr));
While (1)
{
Printf (" waiting for the message... \n");
/* the Receive datas from client */
If ((recv_size=recv (clnt_sock, recvBuf BUF_SIZE, 0))==1)
//error_handling (" fail to receive datas ");
Printf (" % s \ n ", recvBuf);
Memset (recvBuf, 0, BUF_SIZE);

/* send datas to client */
Printf (" Server: ");
The fgets (sendBuf, BUF_SIZE, stdin);
Printf (" sendBuf: % s \ n ", sendBuf);
If ((send_size=send (clnt_sock, sendBuf, BUF_SIZE, 0))==1)
Error_handling (" fail to send datas ");
Printf (" Success to send datas \ n ");
Memset (sendBuf, 0, BUF_SIZE);


}
Close (clnt_sock);
Close (serv_sock);
return 0;


}
Void error_handling (char * message)
{
The fputs (message, stderr);
Fputc (' \ n 'stderr);
The exit (1);
}

Client:
# include
# include
# include
# include
# include
# include
# define BUF_SIZE 1024

Void error_handling (char * message);
Int main (int arg c, char * argv [])
{
Int clnt_sock;
Char sendBuf [BUF_SIZE];
Char recvBuf [BUF_SIZE];
Int send_size;
Int recv_size;
Struct sockaddr_in serv_adr;


If (arg c!
=3){
Usage: printf (" % s \ n ", argv [0]);
The exit (1);
}
//establish a socket
Clnt_sock=socket (PF_INET SOCK_STREAM, 0).
If (clnt_sock==1)
Error_handling (" socket () error!" );
Printf (" the Success to establish the sock \ n ");
//initialize the server address
Memset (& amp; Serv_adr, 0, sizeof (serv_adr));
Serv_adr. Sin_family=AF_INET;
Serv_adr. Sin_addr. S_addr=inet_addr (argv [1]).
Serv_adr. Sin_port=htons (atoi (argv [2]));

If (connect (clnt_sock, (struct sockaddr *) & amp; Serv_adr, sizeof (serv_adr))==1)
Error_handling (" the connect () error!" );
The else
Printf (" Success to connect the socket \ n ");
While (1)
{
Printf (" Input: ");
The fgets (sendBuf, BUF_SIZE, stdin);
If ((send_size=send (clnt_sock, sendBuf, BUF_SIZE, 0))==1)
Error_handling (" the send () error ");
Printf (" sendBuf: % s \ n ", sendBuf);
Memset (sendBuf, 0, sizeof (sendBuf));
If ((recv_size=recv (clnt_sock, recvBuf BUF_SIZE, 0))==1)
Error_handling (" ercv () error ");
Printf (" Server: % s \ n ", recvBuf);
Memset (recvBuf, 0, sizeof (recvBuf));
}

Close (clnt_sock);
return 0;
}

Void error_handling (char * message)
{
The fputs (message, stderr);
Fputc (' \ n 'stderr);
The exit (1);
}
Error is error_handling (" fail to receive datas ");
  • Related