linux socket help

R

Robert Smith

As you can tell by my code that I will post I am obviously new with linux
socket programming so to be to hard on me :)

When I run my little program I get this error:
Server: got connection from 192.168.0.5
recv: Transport endpoint is not connected.

I don't know what that means or why I am getting it.
I know the connection is made by the server: message.

My other end program is a VB program. When i uncomment out the appropriate
lines in the following code my VB app works just find for receiving
messages.

Thanks

Code:
/*
    ** server.c -- a stream socket server demo
    */

    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <errno.h>
    #include <string.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <sys/wait.h>
    #include <signal.h>
    #include <string.h>

    #define MYPORT 3490

    #define BACKLOG 10

    #define MAXDATASIZE 100

    void sigchld_handler(int s)
    {
        while(wait(NULL) > 0);
    }

    int main(void)
    {
        int sockfd, new_fd;
        struct sockaddr_in my_addr;
        struct sockaddr_in their_addr;
        int sin_size;
        struct sigaction sa;
        int yes=1;
 char msg[200]={""};
 int msgLen;
 int numbytes;
 char buf[MAXDATASIZE];



        if ((sockfd = socket(AF_INET, SOCK_STREAM, 0))== -1) {
            perror("socket");
            exit(1);
        }

        if
(setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int))== -1) {
            perror("setsockopt");
            exit(1);
        }

        my_addr.sin_family = AF_INET;
        my_addr.sin_port = htons(MYPORT);
        my_addr.sin_addr.s_addr = INADDR_ANY;
        memset(&(my_addr.sin_zero), '\0', 8);

        if (bind(sockfd, (struct sockaddr *)&my_addr,sizeof(struct
sockaddr)== -1)
         {
            perror("bind");
            exit(1);
        }

        if (listen(sockfd, BACKLOG) == -1) {
            perror("listen");
            exit(1);
        }

        sa.sa_handler = sigchld_handler;
        sigemptyset(&sa.sa_mask);
        sa.sa_flags = SA_RESTART;
        if (sigaction(SIGCHLD, &sa, NULL) == -1) {
            perror("sigaction");
            exit(1);
        }
 sin_size = sizeof(struct sockaddr_in);
            if ((new_fd = accept(sockfd, (structsockaddr *)&their_addr,
&sin_size)) == -1)
      {
                perror("accept");
            }
     printf("server: got connection
from%s\n",inet_ntoa(their_addr.sin_addr));
        while(1)
 {
             //    This code works great sending a message to my other VB
app
     //*********************************************
            //printf("Enter a message to sent: ");
     //gets(msg);
     //msgLen=strlen(msg);
             //   if (send(new_fd, msg, msgLen, 0) ==-1)
             //       perror("send");
             //********************************************

               //this is the code that doesnt work
//*********************************************************************

 if ((numbytes=recv(sockfd, buf, MAXDATASIZE-1, 0)) ==-1)
 {
            perror("recv");
            exit(1);
        }

        buf[numbytes] = '\0';

        printf("Received: %s\n",buf);

        }
//************************************************************************
        return 0;
    }
 
C

CBFalconer

Robert said:
As you can tell by my code that I will post I am obviously new
with linux socket programming so to be to hard on me :)

When I run my little program I get this error:
Server: got connection from 192.168.0.5
recv: Transport endpoint is not connected.

I don't know what that means or why I am getting it.
I know the connection is made by the server: message.

My other end program is a VB program. When i uncomment out the
appropriate lines in the following code my VB app works just
find for receiving messages.

/*
** server.c -- a stream socket server demo
*/

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

Not a standard include file
#include <errno.h>
#include <string.h>
#include <sys/types.h>

Not a standard include file
#include <sys/socket.h>

Not a standard include file
#include <netinet/in.h>

Not a standard include file
#include <arpa/inet.h>

Not a standard include file
#include <sys/wait.h>

Not a standard include file
#include <signal.h>
#include <string.h>

With all those errors we cannot possibly understand your
non-portable code here. Try a newsgroup that deals with your
actual system.
 
A

Ash

if (bind(sockfd, (struct sockaddr *)&my_addr,sizeof(struct
sockaddr)== -1)
{
perror("bind");
exit(1);
}

Here the bind arguments are not proper. The third argument should
contain the size of the address structure whichis sizeof(my_addr).
 
D

Dave Thompson

As you can tell by my code that I will post I am obviously new with linux
socket programming so to be to hard on me :)
Sockets, and (other) Linux specifics, are offtopic in clc, which deals
with standard/portable C. Try comp.unix.programming or (I think it is)
comp.os.linux.development.apps. But:
if ((new_fd = accept(sockfd, (structsockaddr *)&their_addr,
&sin_size)) == -1)
// if (send(new_fd, msg, msgLen, 0) ==-1)
if ((numbytes=recv(sockfd, buf, MAXDATASIZE-1, 0)) ==-1)

Think about which fd is which.

- David.Thompson1 at worldnet.att.net
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top