Linux TCP连接被拒绝

时间:2016-04-24 18:55:17

标签: c linux tcp

我正在进行TCP连接程序,我想将地址和连接步骤设置为函数。程序可以编译,但是当我运行它时,屏幕显示:ERROR CONNECTION:连接被拒绝。请你帮助我好吗?这是服务器代码:

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h> 
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>

void error(char *msg)
{
    perror(msg);
    exit (1);
}
struct sockaddr_in createSockAddr(int portnum)
{
    struct sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_addr.s_addr = INADDR_ANY;
    addr.sin_port = htons(portnum);
    return addr;
}

int createServerSocket(struct sockaddr_in serv_addr)
{   
    int newsockfd;
    int sockfd;
    int clilen;
    sockfd =  socket(AF_INET, SOCK_STREAM, 0);//create socket

    if (sockfd < 0) 
        error("ERROR opening socket");


    //bind socket
    if (bind(sockfd, (struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0) 
        error("ERROR on binding");  
    listen(sockfd,5);//listen
    clilen = sizeof(serv_addr);
    newsockfd = accept(sockfd, (struct sockaddr *) &serv_addr, &clilen);//accept
    if (newsockfd < 0) 
        error("ERROR on accept");

    return newsockfd;
}
int main(int argc, char *argv[])
{
    int sockfd, newsockfd, portnum, clilen;
    char buffer[256];
    struct sockaddr_in serv_addr, cli_addr;
    int n;
    if (argc < 2) {
        fprintf(stderr,"ERROR, no port provided\n");
    exit (2);
    }
    portnum = atoi(argv[1]);
    serv_addr = createSockAddr(portnum);
    newsockfd = createServerSocket(serv_addr);

    bzero(buffer,256);
    n = read(newsockfd,buffer,255);
    if (n < 0) error("ERROR reading from socket");
    printf("Here is the message: %s\n",buffer);
    n = write(newsockfd,"I got your message",18);
    if (n < 0) error("ERROR writing to socket");
    return 0; 
}

以下是客户端代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h> 


void error(char *msg)
{
    perror(msg);
    exit(1);
}
struct sockaddr_in createSockAddr(int portnum)
{

    struct sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_addr.s_addr = inet_addr("127.0.0.1");
    addr.sin_port = htons(portnum);
    return addr;
}

int createClientSocket(struct sockaddr_in sockAddr)
{

    int sockfd;
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd < 0) 
        error("ERROR opening socket");//create socket

    if (connect(sockfd,(struct sockaddr *)&sockAddr,sizeof(sockAddr)) < 0) 
        error("ERROR connecting");
    return sockfd;
}


int main(int argc, char *argv[])
{
    int sockfd, portnum, n;

    struct sockaddr_in serv_addr;
    struct hostent *server;

    char buffer[256];
    if (argc < 3) {
        fprintf(stderr,"usage %s hostname port\n", argv[0]);
        exit(2);
    }
    portnum = atoi(argv[2]);
    serv_addr = createSockAddr(portnum);
    sockfd = createClientSocket(serv_addr);
    printf("Please enter the message: ");
    bzero(buffer,256);
    fgets(buffer,255,stdin);
    n = write(sockfd,buffer,strlen(buffer));
    if (n < 0) 
        error("ERROR writing to socket");
    bzero(buffer,256);
    n = read(sockfd,buffer,255);

    if (n < 0) 
        error("ERROR reading from socket");
    printf("%s\n",buffer);
    return 0;
}

2 个答案:

答案 0 :(得分:1)

我看到的唯一值得怀疑的事情是,在分配字段之前,您没有将sockaddr_in结构归零。否则,它看起来很好。在C中传递结构(而不​​是指向那些结构的指针)有点不典型但是我没有看到它为什么不能工作的任何理由。 (通过&#34;归零&#34;,我的意思是memset(&addr, 0, sizeof(addr)))。)

答案 1 :(得分:1)

代码编译(带有一些小警告),但适用于我(客户端忽略第一个参数并期望第二个端口)。

请再试一次,您还可以在strace的监督下运行服务器和客户端,这是查看应用程序执行的内核系统调用的绝佳工具。