首页 文章

客户端使用C使用TCP套接字将数据发送到端口

提问于
浏览
1

我在C中创建一个必须处理这种情况的客户端程序:

1-服务器程序在端口号为X的客户端发送的端口号8080中接收udp数据报

2-服务器在端口号X中创建一个新的套接字(TCP)

3-使用此TCP套接字,服务器读取客户端发送的字符串

(在localhost上运行)

我不需要制作服务器程序,它已经完成了 . 第1点和第2点都有涵盖,但我已经花了几天时间试图计算出第3点并且我无法使其工作> <

我为客户获得的代码是这样的:

#define MYPORT 8080


int main(int argc, char *argv[ ]) {

int sockfd;

/* connector’s address information */

struct sockaddr_in their_addr;
struct hostent *he;
int numbytes;

int sockfd2, n;
struct sockaddr_in serv_addr;
struct hostent *server;
char buffer[256];


if (argc != 3) {
    fprintf(stderr, "Usage: %s <hostname> <message>\n", argv[0]);
    exit(1);
}

/* get the host info */

if ((he = gethostbyname(argv[1])) == NULL) {
perror("Error obtaining the client. \n");
    exit(1);
}

else printf("Client obtained\n");

if((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
    perror("Error creating UDP socket\n");
    exit(1);
}

else printf("UDP Socket done\n");

their_addr.sin_family = AF_INET;

printf("Port: 8080\n");

their_addr.sin_port = htons(MYPORT);

their_addr.sin_addr = *((struct in_addr *)he->h_addr);

memset(&(their_addr.sin_zero), '\0', 8);

 sockfd2 = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd2 < 0) 
    error("ERROR opening socket");
server = gethostbyname(argv[1]);
if (server == NULL) {
    fprintf(stderr,"ERROR, no such host\n");
    exit(0);
}

//sending port where the TCP socket will be associated
//server client connects correctly to this port 
//and the code it's working fine in this point
if((numbytes = sendto(sockfd, argv[2], strlen(argv[2]), 0, (struct sockaddr *)&their_addr, sizeof(struct sockaddr))) == -1)

{

perror("Client-sendto() error lol!");

exit(1);

}
//port is sent, now let's connect to the port by tcp and write the string
//not working properly from now on

bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr, 
     (char *)&serv_addr.sin_addr.s_addr,
     server->h_length);
serv_addr.sin_port = htons(atoi(argv[2]));
if (bind(sockfd2,(struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) 
    error("ERROR connecting");

listen(sockfd2, 5);
accept(sockfd2, 0, 0);
printf("accepted!\n");

//sending the string to the TCP Port...
if((numbytes = sendto(sockfd2, "hi", 2, 0, (struct sockaddr *)&serv_addr, sizeof(struct sockaddr))) == -1)

{

printf("Client-sendto()-TCP error\n");

exit(1);

}


if (close(sockfd) != 0) printf("Client-sockfd-UDP closing is failed!\n");
else printf("Client-sockfd-UDP successfully closed!\n");
if (close(sockfd) != 0) printf("Client-sockfd2-TCP closing is failed!\n");
else printf("Client-sockfd2-TCP successfully closed!\n");
return 0;

}

代码适用于前两个步骤,但在最后一步,它似乎与TCP端口连接不好,因为我的客户端程序结束但我的服务器程序说他收到null . 当然,我总是发送端口> 1024

在此先感谢,任何帮助将非常感激 .

1 回答

  • 0
    listen(sockfd2, 5);
    accept(sockfd2, 0, 0);
    printf("accepted!\n");
    

    我还没有阅读你的所有代码,但上面(至少)是错误的 . 你绝对需要保留 accept 的返回值:它是你需要写入的套接字!

    accept 返回刚刚创建的新TCP套接字的文件描述符,用于与您的情况下的"server"进行通信 . 您需要将其用作您将字符串写入的文件描述符 .

    (之后的 sendto 调用,除了使用错误的套接字之外,有点可疑,因为服务器无法确定要读取的数据量/消息停止的位置 . 传递长度为3(包括 \0 字节,会有点不怀疑 . )

相关问题