LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-10-2012, 09:08 PM   #1
joet1984
LQ Newbie
 
Registered: Aug 2012
Posts: 10

Rep: Reputation: Disabled
Socket programming in C


I was wondering if anybody can help me out with this. I'm not sure what I am doing wrong. I'm just trying to communicate from 1 unix machine to another using a socket. Here is my code.

Client.c
Code:
#include <sys/types.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include <string.h>

#define BUFFER_LENGTH 250
#define MY_SOCKET "mysocket"

void error(char *msg){
    perror(msg);
    close();
    unlink(MY_SOCKET);
    exit(0);
}

int main(int argc, char **argv){

    int client_sockfd, server_sockfd;
    struct sockaddr_in addr;
    int ip;
    char port;
    char buffer[BUFFER_LENGTH] = "hi joe";

    if(argc != 3){
        printf("Usage: client ip_adress port_number");
        exit(0);
    }

    if(sscanf(argv[1], "%d", &ip) != 1 ){
        printf("Ip address entered is not a number");
        exit(0);
    }

    if((client_sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1){
        error("Socket creation failed");

    }

    memset(&addr, 0, sizeof(struct sockaddr_in));
    addr.sin_family = AF_INET;
    addr.sin_port = inet_addr(argv[2]);
    addr.sin_addr.s_addr = htonl(ip);

    if(bind(client_sockfd, (struct sockaddr *) &addr, (socklen_t) sizeof(struct sockaddr_in)) == -1){
        error("Bind failed");
    }

    if(connect(client_sockfd, (struct sockaddr *) &addr, (socklen_t) sizeof(struct sockaddr_in)) == -1){
        error("Connect failed");
    }

    if(send(client_sockfd, buffer, sizeof(buffer), 0) == -1){
        error("Send failed");
    }

    close();
    exit(0);
}
Server.c
Code:
#include <sys/types.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <arpa/inet.h>

#define BUFFER_LENGTH 250
#define MY_SOCKET "mysocket"

void error(char *msg){
    perror(msg);
    close();
    unlink(MY_SOCKET);
    exit(0);
}

int main(int argc, char **argv){

    int client_sockfd, server_sockfd;
    struct sockaddr_in addr;
    int ip;
    char buffer[BUFFER_LENGTH];
    int backlog = 10;

    if(argc != 3){
        printf("Usage: client port_number");
        exit(0);
    }

    if(sscanf(argv[1], "%d", &ip) != 1 ){
        printf("Ip address entered is not a number");
        exit(0);
    }

    if((server_sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1){
        error("Socket creation failed");
    }    addr.sin_family = AF_INET;
    addr.sin_port = inet_addr(argv[2]);
    addr.sin_addr.s_addr = htonl(ip);

    if(bind(server_sockfd, (struct sockaddr *) &addr, (socklen_t) sizeof(struct sockaddr_in)) == -1){
        error("Bind failed");
    }

    if(listen(server_sockfd, backlog) == -1){
        error("Listen failed");
    }

    if((client_sockfd = accept(server_sockfd, (struct sockaddr *) &addr, (socklen_t *) sizeof(struct sockaddr_in))) == -1){
        error("Accept failed");
    }

    if(recv(client_sockfd, buffer, sizeof(buffer), 0) == -1){
        error("Receive failed");
    }

    int i = 0;
    while(buffer[i] != '\0'){
        printf("%c", buffer[i]);
        i = i + 1;
    }

    unlink(MY_SOCKET);
    exit(0);
}
I run server.c on one computer like so.. ./server 192.(clientaddress) 12345

It hangs and waits like it is suppose to.
Then, I run client.c on another computer ./client 192.(serveraddress) 12345

Output: Connect failed: Invalid argument

Where is the invalid argument I dont get it :[ Any ideas anyone? thanks
 
Old 11-11-2012, 05:21 AM   #2
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Your treating your IP address (e.g. 192.x.x.x) as an int value via the sscanf(). Sure, the sscanf() will succeed; it will scan the first int value it sees, and return that as a result. Thus the IP address that is yielded is 192, not the value you expected.

Hence this statement is incorrect (applies to both the client and server):
Code:
addr.sin_addr.s_addr = htonl(ip);
Although not considered the ideal way to set the IP address, the following is the easiest:
Code:
addr.sin_addr = inet_addr(argv[1]);
Note how I skip around the usage of sscanf(), and just pass the raw input to inet_addr(). If you are concerned about this, then employ sscanf() to ensure that you have a valid IP address.

Ideally you should learn about using getaddrinfo(). I've used it, but I must admit I'm not an expert on the subject. Perhaps you can learn more about this function by reading Beej's Guide to Network Programming.

P.S. Your server is opening AF_INET socket, not an AF_UNIX one. Thus there's no need to unlink() anything at the end of your main(). A socket file is only created when dealing with AF_UNIX.

P.S. #2 In time (ie with experience) you will learn that it is unwise to assume that an entire TCP packet will be received all at once. After all, you are streaming data. For small packets you may not have an issue, but as the packets grow larger, they could very well arrive "in pieces".

Last edited by dwhitney67; 11-11-2012 at 05:22 AM.
 
3 members found this post helpful.
Old 11-12-2012, 10:14 AM   #3
joet1984
LQ Newbie
 
Registered: Aug 2012
Posts: 10

Original Poster
Rep: Reputation: Disabled
Ahhh you're the best. Typical newbie mistake. Thanks. REALLY appreciate it! Great advice at the end too!

Last edited by joet1984; 11-12-2012 at 10:15 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Lab 5: Socket Programming and Pthreads Programming teesha Linux - Newbie 2 02-27-2012 12:25 AM
Help me ... about Socket programming.. rajsun Programming 2 04-24-2005 04:50 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:24 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration