c-server客户端程序参数



你好,我最近收到了这个c中的服务器/客户端程序,它应该是可以的,没有任何错误。问题是,每次我尝试运行它时,当没有足够的参数时,我都会得到程序应该打印的消息(即使我给出了端口号和我希望它回显的消息(,有人能帮我找出我做错了什么吗?提前感谢你给我的任何帮助。我想运行的代码如下:

服务器:

#include <stdio.h> /* for printf() and fprintf() */
#include <sys/socket.h> /* for socket(), connect(), send(), and recv() */
#include <arpa/inet.h> /* for sockaddr_in and inet_addr() */
#include <stdlib.h> /* for atoi() */
#include <string.h> /* for memset() */
#include <unistd.h> /* for close() */
#define MAXPENDING 5 /* Maximum outstanding connection requests */
#define RCVBUFSIZE 32 /*Size of receive buffer*/
void DieWithError(char *errorMessage); /* Error handling function */
void HandleTCPClient(int clntSocket);/* TCP client handling function */

int main(int argc, char *argv[]) {

int servSock; /* Socket descriptor for server */
int clntSock; /* Socket descriptor for client */
struct sockaddr_in echoServAddr; /* Local address */
struct sockaddr_in echoClntAddr; /* Client address */
unsigned short echoServPort; /* Server port */
unsigned int clntLen; /* Length of client address data structure */
if (argc != 2) {/* Test for correct number of arguments */
fprintf(stderr, "Usage: %s <Server Port>n", argv[0]) ;
exit(1);
}
echoServPort = atoi(argv[1]); /* First arg: local port */
/* Create socket for incoming connections */
if ((servSock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
DieWithError( "socket () failed") ;
/* Construct local address structure */
memset(&echoServAddr, 0, sizeof(echoServAddr)); /* Zero out structure */
echoServAddr.sin_family = AF_INET; /* Internet address family */
echoServAddr.sin_addr.s_addr = htonl(INADDR_ANY); /* Any incoming interface */
echoServAddr.sin_port = htons(echoServPort); /* Local port */
/* Bind to the local address */
if (bind(servSock, (struct sockaddr *)&echoServAddr, sizeof(echoServAddr)) < 0)
DieWithError ( "bind () failed");
/* Mark the socket so it will listen for incoming connections */
if (listen(servSock, MAXPENDING) < 0)
DieWithError("listen() failed") ;
for (;;) {/* Run forever */
/* Set the size of the in-out parameter */
clntLen = sizeof(echoClntAddr);
/* Wait for a client to connect */
if ((clntSock = accept(servSock, (struct sockaddr *) &echoClntAddr, &clntLen)) < 0)
DieWithError("accept() failed");
/* clntSock is connected to a client! */
printf("Handling client %sn", inet_ntoa(echoClntAddr.sin_addr));
HandleTCPClient (clntSock) ;
}
/* NOT REACHED */
}
void DieWithError(char *errorMessage){
perror(errorMessage);
exit(1);
}

void HandleTCPClient(int clntSocket) {
char echoBuffer[RCVBUFSIZE];/* Buffer for echo string */
int recvMsgSize;/* Size of received message */

/* Receive message from client */
if ((recvMsgSize = recv(clntSocket, echoBuffer, RCVBUFSIZE, 0)) < 0)
DieWithError("recv() failed") ;
/* Send received string and receive again until end of transmission */
while (recvMsgSize > 0){ /* zero indicates end of transmission */
/* Echo message back to client */
if (send(clntSocket, echoBuffer, recvMsgSize, 0) != recvMsgSize)
DieWithError("send() failed");
/* See if there is more data to receive */
if ((recvMsgSize = recv(clntSocket, echoBuffer, RCVBUFSIZE, 0)) < 0)
DieWithError("recv() failed") ;
}
close(clntSocket); /* Close client socket */
}

客户端如下:


#include <stdio.h> /* for printf() and fprintf() */
#include <sys/socket.h> /* for socket(), connect(), send(), and recv() */
#include <arpa/inet.h> /* for sockaddr_in and inet_addr() */
#include <stdlib.h> /* for atoi() */
#include <string.h> /* for memset() */
#include <unistd.h> /* for close() */
#define RCVBUFSIZE 32 /* Size of receive buffer */
void DieWithError(char *errorMessage); /* Error handling function */
int main(int argc, char *argv[]) {
int sock; /* Socket descriptor */
struct sockaddr_in echoServAddr; /* Echo server address */
unsigned short echoServPort; /* Echo server port */
char *servIP; /* Server IP address (dotted quad) */
char *echoString; /* String to send to echo server */
char echoBuffer[RCVBUFSIZE]; /* Buffer for echo string */
unsigned int echoStringLen; /* Bytes read in single recv()*/
int bytesRcvd, totalBytesRcvd; /*total bytes read */
if ((argc<3) || (argc>4)) {
fprintf(stderr, "Usage: %s <Server IP> <Echo Word> [<Echo Port>]n", argv[0]);
exit(1);
}
servIP = argv[1] ; /* First arg' server IP address (dotted quad) */
echoString = argv[2] ;/* Second arg' string to echo */

if (argc == 4)
echoServPort = atoi(argv[3]); /* Use given port, if any */
else
echoServPort = 7; /* 7 is the well-known port for the echo service */
/* Create a reliable, stream socket using TCP */
if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
DieWithError(" socket () failed") ;
/* Construct the server address structure */
memset(&echoServAddr, 0, sizeof(echoServAddr)); /* Zero out structure */
echoServAddr.sin_family = AF_INET; /* Internet address family */
echoServAddr.sin_addr.s_addr = inet_addr(servIP); /* Server IP address */
echoServAddr.sin_port = htons(echoServPort); /* Server port */
/* Establish the connection to the echo server */
if (connect(sock, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr)) < 0)
DieWithError(" connect () failed") ;
echoStringLen = strlen(echoString) ; /* Determine input length */
/* Send the string to the server */
if (send(sock, echoString, echoStringLen, 0) != echoStringLen)
DieWithError("send() sent a different number of bytes than expected");
/* Receive the same string back from the server */
totalBytesRcvd = 0;
printf("Received: "); /* Setup to print the echoed string */
while (totalBytesRcvd < echoStringLen) {
/* Receive up to the buffer size (minus 1 to leave space for
a null terminator) bytes from the sender */
if ((bytesRcvd = recv(sock, echoBuffer, RCVBUFSIZE - 1, 0)) <= 0)
DieWithError("recv() failed or connection closed prematurely");
totalBytesRcvd += bytesRcvd; /* Keep tally of total bytes */
echoBuffer[bytesRcvd] = ''; /* Terminate the string! */
printf(echoBuffer); /* Print the echo buffer */
}
printf("n"); /* Print a final linefeed */
close(sock);
exit(0);
}
void DieWithError(char *errorMessage){
perror(errorMessage);
exit(1);
}

如果我错了,请纠正我,但我认为问题是您输入了错误的参数,并得到了"用法:…"错误消息?

如果编译正确,程序应该使用以下命令运行:

/服务器1200

/客户端localhost消息1200

服务器程序只接受1个参数,客户端程序接受2或3个参数。服务器也应该在客户端之前运行。

最新更新