当前位置: 技术问答>linux和unix
socket 示例程序疑问
来源: 互联网 发布时间:2016-12-27
本文导语: 编译 gcc -o TCPEchoClient TCPEchoClient.c DieWithError.c 对于编译好的 程序 ./TCPEchoClient 127.0.0.1 "echo this" 如果程序运行结果正常 :Recived :echo this 但是显示 connect refuse TCPEchoClient.c #include /* for printf()...
编译 gcc -o TCPEchoClient TCPEchoClient.c DieWithError.c
对于编译好的 程序 ./TCPEchoClient 127.0.0.1 "echo this"
如果程序运行结果正常 :Recived :echo this
但是显示 connect refuse
TCPEchoClient.c
对于编译好的 程序 ./TCPEchoClient 127.0.0.1 "echo this"
如果程序运行结果正常 :Recived :echo this
但是显示 connect refuse
TCPEchoClient.c
#include /* for printf() and fprintf() */
#include /* for socket(), connect(), send(), and recv() */
#include /* for sockaddr_in and inet_addr() */
#include /* for atoi() and exit() */
#include /* for memset() */
#include /* 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; /* Length of string to echo */
int bytesRcvd, totalBytesRcvd; /* Bytes read in single recv()
and total bytes read */
if ((argc 4)) /* Test for correct number of arguments */
{
fprintf(stderr, "Usage: %s []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(PF_INET, SOCK_STREAM, IPPROTO_TCP))