Thread: problem with sockets on ubuntu.

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    4

    problem with sockets on ubuntu.

    Hello i am new on ubuntu and i coded on windows before. i wanted to code with sockets but i have a problem. i use this example: Minimaler TCP Server & TCP Client in C | Webmaster, Security und Technik Blog

    at first everything works fine
    patti@patti-pc:~/Arbeitsfläche/code/tcp$ ./client
    Sende String: test
    Es wurden 5 Bytes gesendet
    but when i try to change the host and the port to the address of google.de:

    inet_pton(AF_INET, "74.125.39.99", &serveraddr);
    serveraddr.sin_family = AF_INET;
    serveraddr.sin_port = htons(80);
    it wont work. it does not print out anything probably because the program exits after send()
    i think its because connect does not work properly.

    patti@patti-pc:~/Arbeitsfläche/code/tcp$ ./client
    patti@patti-pc:~/Arbeitsfläche/code/tcp$

    sorry for my bad english and i am not a very experienced coder.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    We can't do much without seeing your code. First, make sure you're checking the return value of every IO and socket related function you call (e.g. socket(), send(), etc). Use the perror function to print out a useful error message if a function fails. Then, post your code and the error messages (if any) that your program outputs.

    It's also worth noting that the Windows sockets and the Winsock library were based off of the BSD sockets, which is what Linux uses, so they're very similar. If you've worked with Windows sockets, then you should have a good understanding of what to do in Linux.

    EDIT: I like this network programming guide, but I'm not sure if there's a German translation. And don't worry, your English is pretty good (much better than my German!)

  3. #3
    Registered User
    Join Date
    Jul 2011
    Posts
    4
    i will read this tutorial in a second but first:

    the code is 1:1 to the code of the tutorial both Client and Server only that i changed the ip and the port. but here i placed perror under every operation that could error:
    codepaste

    edit: sorry i forgot to place it under socket() now i did it but its still success

    patti@patti-pc:~/Arbeitsfläche/code/tcp$ ./client
    bzero error: Success
    inet_pton error: Success
    htons error: Success
    error: Connection refused
    patti@patti-pc:~/Arbeitsfläche/code/tcp$
    as you can see it says connection refused after connect.
    but when i try it with telnet> o 74.125.39.103 80 it works
    Last edited by krimix; 07-20-2011 at 02:21 PM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > connect(socks, (struct sockaddr*) &serveraddr, sizeof(serveraddr));
    This also returns a descriptor, and it is this you should be trying to send to.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Jul 2011
    Posts
    4
    if i got this right:
    codepaste

    nope still does not work. but now the program does not hang itself.
    socket error: Success
    bzero error: Success
    inet_pton error: Success
    htons error: Success
    send:-1
    error: Connection refused
    Sende String: test
    Es wurden -1 Bytes gesendet

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by man inet_pton
    AF_INET
    src points to a character string containing an IPv4 network address in dotted-decimal format, "ddd.ddd.ddd.ddd", where ddd is a decimal number of up to three digits
    in the range 0 to 255. The address is converted to a struct in_addr and copied to dst, which must be sizeof(struct in_addr) (4) bytes (32 bits) long.
    The tutorial is wrong. You need to call inet_pton like so:
    Code:
    inet_pton(AF_INET, "74.125.39.103", &(serveraddr.sin_addr));
    Also, Salem was a little mixed up:
    Quote Originally Posted by man connect
    RETURN VALUE
    If the connection or binding succeeds, zero is returned. On error, -1 is returned, and errno is set appropriately.
    You want to send to your socks variable, like so:
    Code:
    int bytes = send(socks, buffer, sizeof(buffer), 0);
    A couple other quick notes:
    1. You need a perror after send()
    2. Your printf statements need a new line at the end. Change the trailing n to a \n:
    Code:
    printf("Sende String: %s\n", buffer);

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Oh, and you probably ought to leave a comment on that tutorial page notifying the author of his/her mistake so they can correct it so nobody else has this problem. I would do it myself, but I don't speak German, and I don't trust an online translator to do a good enough job.

    EDIT: Nevermind, I see a link to this thread in the comments section of the tutorial.
    Last edited by anduril462; 07-20-2011 at 04:03 PM.

  8. #8
    Registered User
    Join Date
    Jul 2011
    Posts
    4
    inet_pton(AF_INET, "74.125.39.103", &(serveraddr.sin_addr));
    thats what i needed thanks man everything works perfect now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ubuntu problem
    By anirban in forum Tech Board
    Replies: 7
    Last Post: 12-30-2010, 02:21 AM
  2. Problem with g++ in Ubuntu 10.04
    By meadhikari in forum C++ Programming
    Replies: 9
    Last Post: 07-13-2010, 06:03 AM
  3. strange ubuntu problem
    By Stonehambey in forum Tech Board
    Replies: 4
    Last Post: 04-21-2009, 11:03 AM