我试图制作一个基本的C程序,以允许我通过telnet连接到简单的tcp服务器。连接后,我将能够输入字符串,并且服务器将设置上限以切换它。
尝试使用telnet时,它将拒绝连接。这是我输入的
telnet localhost 7654
在服务器终端上,我没有收到任何错误,并且终端显示以下内容:
“服务器正在启动...”
和
“服务器现在正在侦听传入的连接”
这是服务器代码:
#include<stdlib.h>
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<netdb.h>
#include<string.h>
#include<signal.h>
#include<errno.h>
#include<ctype.h>
#include <unistd.h>
#define PORTNUMBER 7654
#define BUF_LEN 512
#define COM_LEN 32
void manageConnection(int, int);
int serverProcessing(char *input, char* output);
void handle_sigcld(int);
int main()
{
int mainSocket, tempSocket;
int errorCode, clientLength;
int pid;
struct sockaddr_in server,client;
struct hostent* clientDetails;
struct sigaction cldsig;
fprintf(stderr,"The server is starting up...\n");
/* the following lines of codes are used to prevent zombie processes
from occuring. It allows each childed to be waited on. */
cldsig.sa_handler = handle_sigcld;
sigfillset(&cldsig.sa_mask);
cldsig.sa_flags = SA_RESTART | SA_NOCLDSTOP;
sigaction(SIGCHLD,&cldsig,NULL);
/* creating the socket stream, SOCK_STREAM is a connection based protocol
where a connection is established between and is only disconnected
when a party leaves or network error. */
mainSocket = socket(PF_INET, SOCK_STREAM, 0);
if (mainSocket < 0)
{
perror("Error in function socket()");
exit(EXIT_FAILURE);
}
// setting up the server details by declaring the port number, address family and interface
memset(&server,0,sizeof(server));
server.sin_family = AF_INET;
server.sin_addr.s_addr = htonl(INADDR_ANY);
server.sin_port = PORTNUMBER;
// binding the socket to the server details listed above
if ( (errorCode = bind(mainSocket,(struct sockaddr *)&server,sizeof(server)) ) < 0 )
{
perror("Error in function bind()\n");
exit(EXIT_FAILURE);
}
// put the socket into listen mode so it can listen for incoming connections
if ( (errorCode = listen(mainSocket,5) ) < 0 )
{
perror("Error in function listen()\n");
exit(EXIT_FAILURE);
}
fprintf(stderr,"The server is now listening for incoming connections\n");
while(1)
{
clientLength = sizeof(client);
// accept function is used to extract the first connection and returns a new file discriptor
tempSocket = accept(mainSocket,(struct sockaddr *)&client,(socklen_t *)&clientLength);
if (tempSocket < 0 )
{
perror("Error in function accept()\n");
exit(EXIT_FAILURE);
}
// printing the client connection details
clientDetails = gethostbyaddr( (void*)&client.sin_addr.s_addr,4,AF_INET);
if (clientDetails == NULL)
{
herror("Error in fetching client details\n");
exit(EXIT_FAILURE);
}
fprintf(stderr,"accepted connection from %s on port %d with discriptor %d \n",
clientDetails->h_name,ntohs(client.sin_port),tempSocket);
// this function is used to create a new process to handle the client
if ( (pid = fork() ) == 0)
{
// we close the connection to the main socket and open a sub connection with the temp socket
close(mainSocket);
manageConnection(tempSocket,tempSocket);
exit(EXIT_FAILURE);
}
else
{
close(tempSocket);
}
}
close(mainSocket);
return 0;
}
void manageConnection(int in, int out)
{
int readCount,bufCount;
char inBuf[BUF_LEN], outBuf[BUF_LEN], inData[COM_LEN], hostname[40];
char prefix[100];
char endOfData = '\n';
int i, revCount;
char revBuf[BUF_LEN];
gethostname(hostname,40);
sprintf(prefix,"\tC%d", getpid() );
fprintf(stderr,"\n%s starting up\n",prefix);
sprintf(outBuf,"\n\n connected to TCP server on host: %s \n"\
"enter X to exit otherwise enter the"\
"string to do something cool\n",hostname);
write(out,outBuf,strlen(outBuf));
while(1)
{
bufCount = 0;
while(1)
{
readCount = read(in,inData,COM_LEN);
if (readCount < 0 )
{
if ( (bufCount + readCount) > BUF_LEN)
{
fprintf(stderr,"buffer limit exceeded\n");
close(in);
exit(EXIT_FAILURE);
}
fprintf(stderr,"%sHave read in:\n",prefix);
for(i=0; i<readCount; i++)
{
fprintf(stderr,"%s%d\t%c\n",prefix,inData[i],inData[i]);
}
memcpy(&inBuf[bufCount], inData, readCount);
bufCount=bufCount+readCount;
if (inData[readCount - 1] == endOfData)
{
break;
}
}
else if (readCount == 0 )
{
fprintf(stderr,"\n%s Client has closed connection\n",prefix);
close(in);
exit(EXIT_FAILURE);
}
else
{
sprintf(prefix,"\tC %d: while reading from connection", getpid() );
perror(prefix);
close(in);
exit(EXIT_FAILURE);
}
}
inBuf[bufCount - 2] = '\0';
if (inBuf[0] == 'X')
{
break;
}
revCount = serverProcessing(inBuf,revBuf);
sprintf(outBuf," the server recieved %d characters, which when the string is processed"\
"are:\n%s\n\n enter next string:",strlen(revBuf),revBuf);
write(out,outBuf,strlen(outBuf));
}
fprintf(stderr,"\n%s client has closed the connection\n",prefix);
close(in);
}
int serverProcessing(char* input, char* output)
{
int i, len;
char c;
len=strlen(input);
for(i=0;i<len;i++)
{
c=tolower(input[i]);
if(c==input[i])
{
output[i]=toupper(input[i]);
}
else
{
output[i]=c;
}
}
output[len]='\0';
return len;
}
void handle_sigcld(int sig)
{
pid_t cld;
while ( 0 < waitpid(-1,NULL, WNOHANG) );
}
端口号必须以网络字节顺序给出,并且您的CPU可能没有相应的字节序,以免受到字节顺序的影响替换:
server.sin_port = PORTNUMBER
通过
server.sin_port = htons(PORTNUMBER)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句