我有一个关于 c 语言和套接字编程的作业,我不知道如何开始我要求做的是用 SELECT() 函数(不是 FORK())编写一个 TCP 服务器,服务器需要计算 N 个数字的总和由连接到此服务器的客户端提供。服务器需要打印N个最大的数字和发送这个数字的客户端的IP地址客户端使用终端中的telnet服务连接到服务器
我已经有了 TCP 服务器,但我不知道如何获取客户端发送的数字。谢谢您的帮助
要使用 telnet 服务器,您需要将套接字绑定到端口号。标准的 telnet 端口是 23。由于您已经拥有服务器部分,因此客户端连接到服务器;我会把读取数字的部分发给你。将其复制并粘贴到您的程序中。要使用它,只需使用从接受中获得的套接字调用 ProcessConnection() 函数。
// Some "special" characters definitions
#define RETURN 13
#define NEWLINE 10
#define ESCAPE 27
#define BACKSPACE 8
// this is the max numbers that can be accepted per session
// you may change to do it dynamically, pointers and malloc, case you want to.
#define MAX_NUMS 250
int theNumbers[MAX_NUMS];
// To do the echo of remote typed chars
int doEcho = 0;
// return the sum of the Int theNumbers
int sumThemUp(int count)
{
int sum=0, i=0;
for (i=0; i < count; i++) sum+=theNumbers[i];
return sum;
}
// return the biggest number
int getBiggest(int count)
{
int ret=0, i=0;
for (i=0; i < count; i++)
if (ret < theNumbers[i]) ret = theNumbers[i];
return ret;
}
// return the smallest number
int getSmallest(int count)
{
int ret=0, i=0;
for (i=0; i < count; i++)
if (ret == 0 || ret > theNumbers[i]) ret = theNumbers[i];
return ret;
}
void ProcessConnection(SOCKET wConSock)
{
fd_set sfds;
struct timeval timeout={0, 0};
char actNum[12];
int numNums=0;
char ch;
char * WelcomeMessage1 = "Welcome to my telnet emulator\r\n";
char * WelcomeMessage2 = "Please type in a number and press enter\r\n";
char * WelcomeMessage3 = "To end the session press the Esc key\r\n";
char * WakeUpMessage = "Are you sleeping?, common wake up!!\r\n";
int sentSleep=0;
send(wConSock, WelcomeMessage1, strlen(WelcomeMessage1), 0);
send(wConSock, WelcomeMessage2, strlen(WelcomeMessage2), 0);
send(wConSock, WelcomeMessage3, strlen(WelcomeMessage3), 0);
memset(actNum, 0, sizeof(actNum));
memset(theNumbers, 0, sizeof(int) * MAX_NUMS);
int KeepOnReading = 1;
while (KeepOnReading)
{
FD_ZERO(&sfds);
FD_SET(wConSock, &sfds);
timeout.tv_sec=60; // 1 minute
timeout.tv_usec=0;
switch (select(1, &sfds, NULL, NULL, &timeout))
{
default:
// there are errors in the socket, close it and quit
close(wConSock);
return;
case 0: // time out has elapsed
if (!sentSleep) send(wConSock, WakeUpMessage, strlen(WakeUpMessage), 0);
sentSleep=1;
break;
case 1: // socket has data, let's read it
if (recv(wConSock, &ch, sizeof(ch), 0) == sizeof(ch))
{ // got a char, let's see it
switch (ch)
{
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
if (strlen(actNum) < 10) strncat(actNum, &ch, 1);
if (doEcho) send(wConSock, &ch, sizeof(ch), 0);
sentSleep=0;
break;
case BACKSPACE:
if (strlen(actNum)) actNum[strlen(actNum)-1] = '\0';
if (doEcho) send(wConSock, "\b", 1, 0);
break;
case ESCAPE: // Esc
KeepOnReading=0;
break;
case RETURN:
if (doEcho) send(wConSock, "\r\n", 2, 0);
if (strlen(actNum))
{
theNumbers[numNums] = atoi(actNum);
if (theNumbers[numNums] > 0 && numNums < MAX_NUMS) numNums++;
memset(actNum, 0, sizeof(actNum));
}
break;
}
}
break;
}
}
char text[64];
sprintf(text, "\n\n\r Numbers entered:%10d\r\n", numNums);
send(wConSock, text, strlen(text), 0);
sprintf(text, " Sum of numbers:%10d\r\n", sumThemUp(numNums));
send(wConSock, text, strlen(text), 0);
sprintf(text, " Smallest number:%10d\r\n", getSmallest(numNums));
send(wConSock, text, strlen(text), 0);
sprintf(text, " Biggest number:%10d\r\n", getBiggest(numNums));
send(wConSock, text, strlen(text), 0);
strcpy(text, "\r\n\nPress any key to quit...");
send(wConSock, text, strlen(text), 0);
recv(wConSock, &ch, sizeof(ch), 0);
close(wConSock);
}