简体   繁体   English

套接字程序给出“传输端点已连接”错误

[英]Socket program gives “Transport endpoint is already connected” error

I am trying to create a very simple client-server chat program, where two programs can communicate with each other. 我正在尝试创建一个非常简单的客户端 - 服务器聊天程序,其中两个程序可以相互通信。 However, the accept function is giving me the error "invalid argument". 但是,accept函数给出了错误“invalid argument”。 I am pasting the code here: 我在这里粘贴代码:

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

int main()
{

struct sockaddr_in myaddr;
struct sockaddr_in otheraddr;
int sockid;
int bindid;
int recvsockid;
int clientlen;
int connectid;

char send_msg[100] = "Program 1", recv_msg[100];
int recvid, sendid;
int myport_id = 4550;
int otherport_id = 4560;

sockid = socket(AF_INET, SOCK_STREAM, 0);
//fcntl(sockid, F_SETFL, O_NONBLOCK);

if (sockid < 0)
{
    printf("\nCould not create socket");
}

bzero((char*)&myaddr, sizeof(struct sockaddr_in));
myaddr.sin_family = AF_INET;
myaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
myaddr.sin_port = htons(myport_id);

bzero((char*)&otheraddr, sizeof(struct sockaddr_in));
otheraddr.sin_family = AF_INET;
otheraddr.sin_addr.s_addr = inet_addr("127.0.0.1");
otheraddr.sin_port = htons(otherport_id);


bindid = bind(sockid, (struct sockaddr*)&myaddr, sizeof(struct sockaddr_in));
if(bindid < 0)
    printf("bind error \n");

listen(bindid, 5);

do
{
    connectid = connect(sockid, (struct sockaddr*)&otheraddr, sizeof(struct sockaddr_in));
if (connectid < 0 && !(errno == EINPROGRESS))
{
    //printf("%s", strerror(errno));
    perror("connect");
    exit(1);
}
    recvsockid = accept(sockid, (struct sockaddr*)&myaddr, &clientlen);
if (recvsockid < 0 && !(errno == EINPROGRESS || errno == EAGAIN))
{
    perror("accept");
    exit(1);
}
} while (connectid < 0 && recvsockid < 0);

do
{
    gets(send_msg);
    sendid = sendto(sockid, send_msg, 100, 0, (struct sockaddr*)&otheraddr, sizeof(struct sockaddr_in));
    fprintf(stderr, "%d", sendid);
    if(sendid < 0)
        printf("error3\n");

    recvid = recvfrom(recvsockid, recv_msg, 100, 0, (struct sockaddr*)&myaddr, &clientlen);
    if (recvid < 0)
    {
        printf("\nError in receive");
        break;
    }

} while (1);

return 0;
}

I will be grateful if someone could tell me why I am getting this error, and how to correct it. 如果有人能告诉我为什么会收到此错误,以及如何纠正错误,我将不胜感激。

Thanks a lot. 非常感谢。

Your problem is that you are trying to connect and to listen on the same socket sockid . 您的问题是您正在尝试连接并侦听同一个套接字sockid I'm pretty sure you meant bindid for listening. 我很确定你的意思是bindid用于倾听。

Edit 0: 编辑0:

Since you are creating both sides of the TCP connection in the same program, you need two socket descriptors, ie two calls to socket(2) in the setup code, one for connecting and one for accepting client connections. 由于您在同一程序中创建TCP连接的两端,因此需要两个套接字描述符,即在设置代码中对socket(2)两次调用,一次用于连接,一次用于接受客户端连接。

recvsockid = accept(sockid, (struct sockaddr*)&myaddr, &clientlen);

You have not initialized clientlen , if you look at the documentation for accept(): 如果你查看accept()的文档,你还没有初始化clientlen

address_len Points to a socklen_t structure which on input specifies the length of the supplied sockaddr structure, and on output specifies the length of the stored address. address_len指向一个socklen_t结构,该结构在输入时指定所提供的sockaddr结构的长度,并在输出中指定存储地址的长度。

So, set it to the length of myaddr prior to calling accept(): 因此,在调用accept()之前将其设置为myaddr的长度:

client_len = sizeof myaddr;
recvsockid = accept(sockid, (struct sockaddr*)&myaddr, &clientlen);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM