Problem using sockets

Discussion in 'C Programming' started by Vlad Dogaru, Jan 14, 2007.

  1. Vlad Dogaru

    Vlad Dogaru Guest

    Hello,

    I am trying to write a simple program to teach myself sockets. The
    following bit of code fails with:
    "connect: Socket operation on non-socket". What am I missing?

    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <errno.h>
    #include <strings.h>
    #include <sys/select.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    #include <netdb.h>

    int main(int argc, char **argv)
    {
    int s1, s2; /* socket descriptors */
    extern int errno;
    struct sockaddr_in sa1, sa2;
    char aux[MAX_STR_LEN];
    struct hostent *hp;

    if (argc != 5) {
    printf("Usage: %s <host1> <port1> <host2> <port2>\n", argv[0]);
    return 0;
    }

    if ((hp=gethostbyname(argv[1])) == 0) {
    perror("gethostbyname");
    return errno;
    }

    bzero(&sa1, sizeof sa1);
    sa1.sin_family = AF_INET;
    sa1.sin_port = htons(atoi(argv[2]));
    sa1.sin_addr.s_addr = ((struct in_addr *)(hp->h_addr))->s_addr;

    if ((s1=socket(AF_INET, SOCK_STREAM, 0) == -1)) {
    perror("socket");
    return errno;
    }

    if (connect(s1, (void *) &sa1, sizeof (sa1)) == -1) {
    perror("connect");
    return errno;
    }
    printf("Socket 1 connected");

    Also, having two sockets open, can I use select() to read from them?
    Can someone please point me to an example of using select() with
    sockets?

    Thanks in advance,
    Vlad
     
    Vlad Dogaru, Jan 14, 2007
    #1
    1. Advertising

  2. "Vlad Dogaru" <> writes:
    > I am trying to write a simple program to teach myself sockets.

    [...]

    Standard C does not include support for sockets.
    Try comp.unix.programmer.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Jan 14, 2007
    #2
    1. Advertising

  3. On 14 Jan 2007 01:50:30 -0800, "Vlad Dogaru" <> wrote:

    > Hello,
    >
    > I am trying to write a simple program to teach myself sockets. The
    > following bit of code fails with:
    > "connect: Socket operation on non-socket". What am I missing?
    >

    The sockets part is offtopic but your problem is actually a C problem.

    > if ((s1=socket(AF_INET, SOCK_STREAM, 0) == -1)) {


    This compares the return value from socket(...) to -1 to choose
    whether to assign 0 or possibly 1 to s1. Neither of those is a valid
    sd (in your obviously Unixoid environment) and even if valid wouldn't
    be correct i.e. the value you want later. You need to move one of
    your parentheses: if ( (s1=socket(...)) == -1 ) { error }

    - David.Thompson1 at worldnet.att.net
     
    Dave Thompson, Jan 29, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?U3llZCBBdW4gQWxp?=

    how to communicate server/client using tcpip and /or sockets

    =?Utf-8?B?U3llZCBBdW4gQWxp?=, Aug 7, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    492
    Steve C. Orr [MVP, MCSD]
    Aug 7, 2004
  2. Nachhu
    Replies:
    1
    Views:
    476
    Bryan Bullard
    Jan 12, 2004
  3. Paul Morrison

    File transfer using sockets

    Paul Morrison, Oct 19, 2004, in forum: Java
    Replies:
    3
    Views:
    14,401
    John Davison
    Oct 19, 2004
  4. Ken Adams
    Replies:
    3
    Views:
    1,007
    Tilman Bohn
    Dec 9, 2004
  5. Dundonald
    Replies:
    2
    Views:
    397
    Steve Horsley
    Mar 15, 2005
Loading...

Share This Page