Telnet拒绝简单TCP服务器上的连接

威廉_

我试图制作一个基本的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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

拒绝到TCP服务器的传入连接

无法连接到服务器:连接被拒绝服务器是否在主机“ 127.0.0.1”上运行并接受端口5432上的TCP / IP连接?

Raspberry Pi上的Django开发服务器拒绝连接

与服务器localhost:8080的连接被拒绝

IRC服务器拒绝我的连接

Websocket服务器连接被拒绝

Netcat服务器连接被拒绝

JMeter连接Apache服务器被拒绝

Domino 服务器拒绝连接

无法连接到服务器:连接被拒绝(0x0000274D / 10061)-远程服务器上的PostgreSQL

django postgres 无法连接到服务器:连接被拒绝服务器是否在主机“localhost”(127.0.0.1)上运行

无法使用go和docker连接到mysql服务器-拨打TCP 127.0.0.1:3306:连接:连接被拒绝

OperationalError:无法连接到服务器:端口5432上的权限拒绝tTCP / IP连接?

Netty 4呼叫被阻止,简单的Telnet服务器

无法连接到服务器:连接被拒绝 服务器是否在主机上运行...并在端口 5432 上接受 TCP/IP 连接?

与远程服务器的TCP连接

在我用 Java 创建一个简单的服务器后,Telnet 不会连接到本地主机

通过Telnet连接到POP / SMTP服务器

Telnet连接到传出SMTP服务器

将用户连接到telnet服务器的Rails应用

连接到NodeJS中的telnet服务器

AWS上的Kubernetes集群,到服务器localhost:8080的连接被拒绝

使用socket.io的DigitalOcean上的NodeJS服务器返回拒绝的连接

尝试连接到蓝牙服务器时连接被拒绝

尝试连接到服务器时,Sshutle连接被拒绝

PulseAudio服务器连接失败:连接被拒绝

通过VPN连接时,FTP服务器连接被拒绝

PG :: ConnectionBad:无法连接到服务器:连接被拒绝

连接到 Web 服务器时连接被拒绝