如果我从两个不同的shell运行客户端程序,服务器应该如何确定他当前与谁交谈?
平台:Linux,GCC
客户端代码:此客户端将主机名作为参数。
#define SERVERPORT 3490
#define MAXDATASIZE 1000
int main (int argc, char *argv[])
{
int clientSocketFD;
int numOfBytesReceived;
char receivedData[MAXDATASIZE];
struct hostent *objHostent;
struct sockaddr_in serverAddress;
if (argc != 2)
{
fprintf (stderr,"usage: key in client hostname.\n");
exit (1);
}
if ((objHostent = gethostbyname (argv[1])) == NULL)
{
herror ("error: incorrect host name specified.");
exit (1);
}
if ((clientSocketFD = socket (PF_INET, SOCK_STREAM, 0)) == -1)
{
perror ("error: socket system call failed.");
exit (1);
}
serverAddress.sin_family = AF_INET;
serverAddress.sin_port = htons (SERVERPORT);
serverAddress.sin_addr = *((struct in_addr *)objHostent->h_addr);
memset (serverAddress.sin_zero, '\0', sizeof serverAddress.sin_zero);
if (connect (clientSocketFD, (struct sockaddr *)&serverAddress, sizeof serverAddress) == -1)
{
perror ("connect() error");
exit (1);
}
while (1)
{
if ((numOfBytesReceived = recv (clientSocketFD, receivedData, MAXDATASIZE-1, 0)) == -1)
{
perror ("\nrecv() error");
}
receivedData [numOfBytesReceived] = '\0';
if (send (clientSocketFD, "Get Lost", 15, 0) == -1)
{
perror ("send() error");
}
}
close (clientSocketFD);
return 0;
}
服务器代码:
#define SERVERPORT 3490
#define BACKLOG 10
struct threadArguments
{
int threadId;
int listeningSocketDescriptor;
char *message;
};
struct threadArguments threadDataArray [5];
void* genericThreadFunction (void* threadData)
{
struct threadArguments *temp;
temp = (struct threadArguments *) threadData;
printf ("Hello World! It's me, thread #%ld!\n", (long) temp->threadId);
while (1)
{
if (send (temp->listeningSocketDescriptor, temp->message, 14, 0) == -1)
perror ("\nsend() error\n");
char receivedData [14];
int numOfBytesReceived;
if ((numOfBytesReceived = recv (temp->listeningSocketDescriptor, receivedData, 14-1, 0)) == -1)
{
perror ("\nrecv() error\n");
}
else
{
receivedData [numOfBytesReceived] = '\0';
printf ("\nReceived: %s\n", receivedData);
}
}
}
int main (void)
{
int serverSocketFD;
int newServersocketFD;
struct sockaddr_in serverAddress;
struct sockaddr_in clientAddress;
socklen_t sin_size;
if ((serverSocketFD = socket (AF_INET, SOCK_STREAM, 0)) == -1)
{
perror ("socket");
exit (1);
}
int yes=1;
if (setsockopt (serverSocketFD, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1)
{
perror ("setsockopt");
exit (1);
}
serverAddress.sin_family = AF_INET; // host byte order
serverAddress.sin_port = htons (SERVERPORT); // short, network byte order
serverAddress.sin_addr.s_addr = INADDR_ANY; // automatically fill with my IP
memset (serverAddress.sin_zero, '\0', sizeof serverAddress.sin_zero);
if (bind (serverSocketFD, (struct sockaddr *)&serverAddress, sizeof serverAddress) == -1)
{
perror ("bind");
exit (1);
}
if (listen (serverSocketFD, BACKLOG) == -1)
{
perror ("listen");
exit (1);
}
pthread_t threads [5];
int returnValueOfPthreadCreate;
long threadId = -1;
while (1)
{
sin_size = sizeof clientAddress;
if ((newServersocketFD = accept (serverSocketFD, (struct sockaddr *)&clientAddress, &sin_size)) == -1)
{
perror ("accept");
continue;
}
printf ("accept(): got connection from %s\n", inet_ntoa (clientAddress.sin_addr));
threadId++;
threadDataArray [threadId].threadId = threadId;
threadDataArray [threadId].listeningSocketDescriptor = newServersocketFD;
threadDataArray [threadId].message = "Excuse Me, please!";
returnValueOfPthreadCreate = pthread_create (&threads [threadId], NULL, genericThreadFunction, (void*) &threadDataArray[threadId]);
if (returnValueOfPthreadCreate)
{
printf ("ERROR; return code from pthread_create() is %d\n", returnValueOfPthreadCreate);
}
}
pthread_exit (NULL);
return 0;
}
Peername返回0:
if ((newServersocketFD = accept (serverSocketFD, (struct sockaddr *)&clientAddress, &sin_size)) == -1)
{
perror ("accept");
continue;
}
printf ("accept(): got connection from %s\n", inet_ntoa (clientAddress.sin_addr));
printf ("\npeername: %d", getpeername (newServersocketFD, (struct sockaddr *)&clientAddress, &sin_size));
答案 0 :(得分:6)
客户端的ip-address和( source - )端口(如下所述获取)执行唯一标识客户端(-connection)。
如果您使用地址系列IF_INET
来创建listen()
套接字,您可以从类型{的变量中提取客户端的ip-address和(客户端的特定于连接)(source-)端口{1}}每次struct sockaddr_in
成功返回时,使用其成员accept()
和sin_addr
将参考文件传递给sin_port
。
根据您所使用的平台,您需要更改此类成员的字节顺序(有关如何执行此操作,请参阅accept()
的手册页)。要将成员ntoh()
直接转换为字符数组,您已经在使用方法(sin_addr
)来处理字节顺序。
在成功inet_ntoa()
返回的文件描述符上使用getpeername()
(在服务器进程的上下文中,也唯一标识客户端的连接)应该对accept()
的调用返回与对等体的IP地址和端口相同的值。
答案 1 :(得分:2)
每个客户端都有一个单独的已接受套接字(代码中为newServersocketFD
)
如果您在每个上使用getpeername()
,您会发现他们使用不同的源端口。