Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
09-20-2013, 05:29 PM
|
#1
|
LQ Newbie
Registered: Dec 2004
Distribution: suse & mandrake
Posts: 6
Rep:
|
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
|
|
|
09-21-2013, 09:51 AM
|
#2
|
Moderator
Registered: Aug 2002
Posts: 26,512
|
Have you verified that the firewalls if any are running allow traffic on the desired port?
|
|
1 members found this post helpful.
|
09-24-2013, 01:54 PM
|
#3
|
LQ Newbie
Registered: Dec 2004
Distribution: suse & mandrake
Posts: 6
Original Poster
Rep:
|
Jip, that was the problem, the port was closed during an automatic 'update'
Quote:
Originally Posted by michaelk
Have you verified that the firewalls if any are running allow traffic on the desired port?
|
Thanks
|
|
|
09-24-2013, 02:06 PM
|
#4
|
Moderator
Registered: Aug 2002
Posts: 26,512
|
Thanks for posting back that it works. You can mark the thread as solved under thread tools at the top.
|
|
|
All times are GMT -5. The time now is 06:26 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|