简体   繁体   中英

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". 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 . I'm pretty sure you meant bindid for listening.

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

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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