connect to host in C

I have to connect to host and port. Port is const, but host variable should input from shell. I've got an error.

int main(int argc, char ** argv)
{
    if (argc<2){
        printf("Need more arguments\n");
        return 1;
    }
    int sock;
    if((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0){
        perror("socket creating error");
        return 1;
    }

    struct sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(22222);
    addr.sin_addr.s_addr = argv[1];    // try to conect host
...
}

Answers


You can't assign the host to the s_addr like that. You need to convert it from a string host name/ip number to the correct format for s_addr

If you're POSIX, look at getaddrinfo() otherwise you can use things like inet_addr(), gethostbyname(), gethostbyaddr() and so on. The key being, the need to make the conversion.

I would suggest a google for some example client/server code... there's a ton of it. Including here on SO.


Need Your Help

Trouble with OQL 'unique' function of 'jhat' in Java 1.6.0_24

java jhat oql

this is my first posted question. I have a fairly complicated OQL query which lists static fields of classes loaded in our packages. One problem I'm seeing with running the query against 'jhat' of ...

Populate checkbox value in ASP.NET MVC

jquery asp.net-mvc checkbox

I want to checked default both values. So I used the following code in jQuery..

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.