LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices


Reply
  Search this Thread
Old 09-20-2013, 05:29 PM   #1
creatron
LQ Newbie
 
Registered: Dec 2004
Distribution: suse & mandrake
Posts: 6

Rep: Reputation: 0
Socket: Client/Server prog not working on different PCs, but if run same PC it works


I wanted to send data from one linux app (finger reader) to an PC using sockets. The finger reader has very limited resources, and I choosen to use UDP packets to send data to a PC. I downloaded code for "Beginning Linux Programming" for both clients and servers, and it works fine, but only on the same platform. Using wireshark I can see that client data (finger reader) data is reached on the server PC, but the application just ignores it, the client retries and later timeout. I can ping from both sides, so I do have comms (wireshark confirms it). I also tried TCP and used netcat with simular results. The two 'devices' are connected via a switch. I also tried this on Ubuntu 13.04 and OpenSuse 12.3. I don't see any 'problem' with data data as displayed in wireshark (used ip.addr==xx or udp.port or tcp.port etc as filter)
NetCat (alias nc)
# (server) nc -u -l -v 1234 #(client) nc -u 192.168.2.64 1234 , same PC Fine
# (server) nc -l -v 1234 #(client) nc 192.168.2.64 1234 ,same PC Fine
differents PCs .. no go.

Client software
Code:
/*  Make the necessary includes and set up the variables.  */

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

int main()
{
    int sockfd;
    int len;
    struct sockaddr_in address;
    int result;
    char ch = 'A';

/*  Create a socket for the client.  */

    sockfd = socket(AF_INET, SOCK_STREAM, 0);

/*  Name the socket, as agreed with the server.  */

    address.sin_family = AF_INET;
    address.sin_addr.s_addr = inet_addr("192.168.2.52");
    address.sin_port = htons(9734);
    len = sizeof(address);

/*  Now connect our socket to the server's socket.  */

    result = connect(sockfd, (struct sockaddr *)&address, len);

    if(result == -1) {
        perror("oops: client3");
        exit(1);
    }

/*  We can now read/write via sockfd.  */

    write(sockfd, &ch, 1);
    read(sockfd, &ch, 1);
    printf("char from server = %c\n", ch);
    close(sockfd);
    exit(0);
}
Server Code
Code:
/*  This program, server4.c, begins in similar vein to our last server,
    with the notable addition of an include for the signal.h header file.
    The variables and the procedure of creating and naming a socket are the same.  */

#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <netinet/in.h>
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>

int main()
{
    int server_sockfd, client_sockfd;
    int server_len, client_len;
    struct sockaddr_in server_address;
    struct sockaddr_in client_address;

    //server_sockfd = socket(AF_INET, SOCK_STREAM, 0);
    server_sockfd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);

    server_address.sin_family = AF_INET;
    server_address.sin_addr.s_addr = htonl(INADDR_ANY);
    server_address.sin_port = htons(9734);
    server_len = sizeof(server_address);
    bind(server_sockfd, (struct sockaddr *)&server_address, server_len);

/*  Create a connection queue, ignore child exit details and wait for clients.  */

    listen(server_sockfd, 5);

    signal(SIGCHLD, SIG_IGN);

    while(1) {
        char ch;

        printf("server waiting\n");

/*  Accept connection.  */

        client_len = sizeof(client_address);
        client_sockfd = accept(server_sockfd, 
            (struct sockaddr *)&client_address, &client_len);

/*  Fork to create a process for this client and perform a test to see
    whether we're the parent or the child.  */

        if(fork() == 0) {

/*  If we're the child, we can now read/write to the client on client_sockfd.
    The five second delay is just for this demonstration.  */

            read(client_sockfd, &ch, 1);
	    printf (">> Rx %c ",ch);
            //sleep(5);
            ch++;
            write(client_sockfd, &ch, 1);
            printf (" Tx %c ",ch);
            close(client_sockfd);
            exit(0);
        }

/*  Otherwise, we must be the parent and our work for this client is finished.  */

        else {
            close(client_sockfd);
        }
    }
}
Thanks in advance
 
Old 09-21-2013, 09:51 AM   #2
michaelk
Moderator
 
Registered: Aug 2002
Posts: 26,512

Rep: Reputation: 6223Reputation: 6223Reputation: 6223Reputation: 6223Reputation: 6223Reputation: 6223Reputation: 6223Reputation: 6223Reputation: 6223Reputation: 6223Reputation: 6223
Have you verified that the firewalls if any are running allow traffic on the desired port?
 
1 members found this post helpful.
Old 09-24-2013, 01:54 PM   #3
creatron
LQ Newbie
 
Registered: Dec 2004
Distribution: suse & mandrake
Posts: 6

Original Poster
Rep: Reputation: 0
Smile Jip, that was the problem, the port was closed during an automatic 'update'

Quote:
Originally Posted by michaelk View Post
Have you verified that the firewalls if any are running allow traffic on the desired port?

Thanks
 
Old 09-24-2013, 02:06 PM   #4
michaelk
Moderator
 
Registered: Aug 2002
Posts: 26,512

Rep: Reputation: 6223Reputation: 6223Reputation: 6223Reputation: 6223Reputation: 6223Reputation: 6223Reputation: 6223Reputation: 6223Reputation: 6223Reputation: 6223Reputation: 6223
Thanks for posting back that it works. You can mark the thread as solved under thread tools at the top.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Nis Client On Centos not working with Suse Server . But works with Suse Nis Client jibinforu Linux - Server 2 07-23-2009 09:44 PM
Nis Client On Centos not working with Suse Server . But works with Suse Nis Client jibinforu Linux - Networking 1 07-13-2009 06:51 AM
Client pcs cannot resolve IP from DHCP server shipon_97 Linux - Enterprise 1 08-09-2007 01:54 PM
cannot read data at server socket, though client socket sends it jacques83 Linux - Networking 0 11-15-2005 02:58 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Networking

All times are GMT -5. The time now is 06:26 AM.

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