插座计划为"交通运输终端已经连接"错误 [英] Socket program gives "Transport endpoint is already connected" error

查看:217
本文介绍了插座计划为"交通运输终端已经连接"错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个非常简单的客户端 - 服务器的聊天程序,其中两个程序可以相互通信。然而,接受功能是给我的错误无效的参数。我在这里粘贴code:

 的#include<&stdio.h中GT;
#包括LT&; SYS / types.h中>
#包括LT&; SYS / socket.h中>
#包括LT&; netinet / in.h中>
#包括LT&;&string.h中GT;
#包括LT&;&fcntl.h GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&errno.h中GT;诠释的main()
{结构SOCKADDR_IN MYADDR;
结构SOCKADDR_IN otheraddr;
INT sockid;
INT bindid;
INT recvsockid;
INT clientlen;
INT connectid;炭send_msg [100] =节目1,recv_msg [100];
INT recvid,sendid;
INT myport_id = 4550;
INT otherport_id = 4560;sockid =插座(AF_INET,SOCK_STREAM,0);
//的fcntl(sockid,F_SETFL,O_NONBLOCK);如果(sockid℃,)
{
    的printf(\\ n无法创建套接字);
}bzero((字符*)及MYADDR,的sizeof(结构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((字符*)及otheraddr,的sizeof(结构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 =绑定(sockid,(结构sockaddr *)及MYADDR,的sizeof(结构SOCKADDR_IN));
如果(bindid℃,)
    的printf(绑定错误。\\ n);听(bindid,5);做
{
    connectid =连接(sockid,(结构sockaddr *)及otheraddr,的sizeof(结构SOCKADDR_IN));
如果(connectid℃,与放大器;&安培;!(错误== EINPROGRESS))
{
    //的printf(%S,字符串错误(错误));
    PERROR(连接);
    出口(1);
}
    recvsockid =接受(sockid,(结构sockaddr *)及MYADDR,&安培; clientlen);
如果(recvsockid℃,与放大器;&安培;!(错误== EINPROGRESS ||错误号== EAGAIN))
{
    PERROR(接受);
    出口(1);
}
}而(connectid&℃,放大器;&放大器; recvsockid℃下);做
{
    得到(send_msg);
    sendid = SENDTO(sockid,send_msg,100,0,(结构sockaddr *)及otheraddr,的sizeof(结构SOCKADDR_IN));
    fprintf中(标准错误,%D,sendid);
    如果(sendid℃,)
        的printf(误差3 \\ n);    recvid = recvfrom的(recvsockid,recv_msg,100,0,(结构sockaddr *)及MYADDR,&安培; clientlen);
    如果(recvid℃,)
    {
        的printf(\\ n错误在接收);
        打破;
    }}而(1);返回0;
}

我会很感激,如果有人能告诉我为什么我收到此错误,以及如何纠正它。

非常感谢。


解决方案

  recvsockid =接受(sockid,(结构sockaddr *)及MYADDR,&安培; clientlen);

您还没有初始化 clientlen ,如果你看一下文档accept()方法:


  

address_len
                指向一个socklen_t的结构,输入指定供应sockaddr结构体的长度,并在指定的输出
  长度存储的地址。


因此​​,将其设置为调用accept()之前的长度 MYADDR

  client_len = sizeof的MYADDR;
recvsockid =接受(sockid,(结构sockaddr *)及MYADDR,&安培; clientlen);

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". 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.

解决方案

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

You have not initialized clientlen, if you look at the documentation for accept():

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.

So, set it to the length of myaddr prior to calling accept():

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

这篇关于插座计划为&QUOT;交通运输终端已经连接&QUOT;错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆