gpt4 book ai didi

客户端套接字不会在聊天客户端中使用 socket() 函数打开

转载 作者:太空宇宙 更新时间:2023-11-04 09:48:48 24 4
gpt4 key购买 nike

所以这是一项正在进行的工作,但是当我运行命令时:./服务器 4444从不同的终端窗口,./client localhost 4444

客户端在“打开套接字时出错”处停止。我不知道为什么,任何建议将不胜感激。我完全知道这段代码有错误且不完整,但如果不先让我的客户端和服务器正确连接,我就无法继续前进。

这是我的代码:

客户端.c

#include "../lib/sockettalk.h"
#include "../lib/my.h"
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string.h>
#include <stdio.h>

//#define MAX_SIZE=255

int main(int argc, char **argv)
{
int sockfd = socket(AF_INET, SOCK_STREAM, 0);//only need one socket since you are the only one connecting
int n;
char buffer[MAX_SIZE];
struct sockaddr_in serv_addr;
struct hostent *server;
int portnum = atoi(argv[2]);
if(argc < 3)
{
printf("Check your arguments.");
exit(0);
}

//printf("%d", portnum);
if(portnum < 2000 || portnum > 65535)//must be greater than 5000 to avoid conflictin ports
{
printf("Error, port number is out of bounds. Must be >2000 AND <65535.");
printf("Please enter a valid port number next time.");
exit(0);
}

if(sockfd < 0);
printf("Error opening socket. \n");
else
printf("Opened socket directly. \n");

if((server = gethostbyname(argv[1])) == 0)
{
printf("Error, no host. \n");
exit(0);
}

//memset(&serv_addr, '0', sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htonl(portnum);

if((connect(sockfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr))) < 0)
{
printf("Error connecting. \n");

}

while(1)
{
//clear and write a message
memset(buffer, '0', sizeof(buffer));
if((n = write(sockfd, buffer, my_strlen(buffer))) < 0)
{
printf("Error writing to server. \n");
exit(1);
}
printf("Client: ");
printf("%s", buffer);
printf(" \n");

//clear and read
memset(buffer, '0', sizeof(buffer));
if((n = read(sockfd, buffer, MAX_SIZE)) < 0)
{
printf("Error reading from server. \n");
exit(1);
}
printf("Server: ");
printf("%s", buffer);
printf(" \n");
}
return 0;
}

服务器.c

#include "../lib/my.h"
#include "../lib/sockettalk.h"
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <string.h>
#include <stdio.h>


int main(int argc, char **argv)
{
int port = atoi(argv[1]);
int pid;
int n; //to read and write
char buffer[MAX_BUFFER_SIZE];
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
int newsockfd;
int clilen; //to be used in accept step as a dummy, need size of bytes
struct sockaddr_in cli_addr, serv_addr;
memset(&serv_addr, 0, sizeof(serv_addr));

if (sockfd<0)
printf("Problem openning socket.\n");
else
{
printf("Openned socket successfully.\n");
}

serv_addr.sin_family = AF_INET; //address family
serv_addr.sin_port = port; //IP port
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY); //IP address, INADDR_ANY allows program to work without knowing the IP address of machine it is running on.

if(bind(sockfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0)//assigns a name to a socket. we must also cast serv_addr to a pointer to a struct socketDDR
{
printf("Error binding socket. \n");
exit(1);
}
else
{
printf("Bound socket successfully. \n");
}

if(listen(sockfd, 5) < 0) //should be an already bound socket. 5 is the number of clients that can connect at once
{
printf("Error listning.\n");
exit(1);
}

while(1) //place into loop so that process can repeat for every new connection
{
//now a newsockfd needs to be created so that it gives every new client a unique identifir
newsockfd = accept(sockfd,(struct sockaddr*) &cli_addr, &clilen);
if(newsockfd < 0)
{
printf("Error accepting\n");
exit(1);
}
else
{
printf("Client is connected.\n");
}

if ((pid = fork()) < 0) //problem
{
printf("Error forking. \n");
exit(1);
}
else if(pid == 0) //forking is sucessful, is a child
{
close(sockfd); //closes from the child side
while(1)
{
memset(buffer, 0, sizeof(buffer));
n = read(newsockfd, buffer, MAX_BUFFER_SIZE);
if (n < 0)
{
printf("Error reading from client.\n");
exit(1);
}
printf("%s", buffer);
printf("\n");

memset(buffer, 0, sizeof(buffer));
fgets(buffer, 254, stdin);
n = write(newsockfd, buffer, strlen(buffer));
if(n < 0)
{
printf("Error writing to client.\n");
exit(1);
}
printf("Server: ");
printf("%s", buffer);
printf("\n");
}
close(sockfd);
}
}
return 0;
}

最佳答案

在客户端中,您实际上并没有在任何地方使用 server = gethostbyname(...) 的结果。实际上,您未初始化 serv_addr.sin_addr,因此您试图连接到某个随机 IP 地址(可能是 0.0.0.0),这当然会失败。

关于客户端套接字不会在聊天客户端中使用 socket() 函数打开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13372794/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com