LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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 07-14-2012, 05:58 PM   #1
matiasar
Member
 
Registered: Nov 2006
Location: Argentina
Distribution: Debian
Posts: 314
Blog Entries: 1

Rep: Reputation: 31
Probem with Linux Socket and Windows client


I'm learning how to write and use sockets following guide of Nathan Yocom's book: "The Definitive Guide to Linux Network Programming".
I'm working in a little application which uses Yocom's methodology to build a listening socket for multiple clients connections using fork system call.

Socket works just well as long as I use a Linux client too. If client is a linux box, there's no problem at all. But there's a weird behaviour when I connect to the Linux server socket from a windows box. I do telnet IP_OF_HOST 1972 and as long as I type any character socket disconnect the client.
I didn't find any advince within book related to windows client.
Does anybody knows what could be wrong?

Code:
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <string.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <sys/wait.h>
#include <signal.h>

//void child_func(int childnum);
void sigchld_handler(int signo);

int main (int argc, char *argv[]) 
{
	struct sockaddr_in sAddr;
	int listensock;
	int newsock;
	char buffer[25];
	int result;
	int nread;
	int pid;
	int val;

	listensock = socket(AF_INET,SOCK_STREAM, IPPROTO_TCP);
	val = 1;

	result = setsockopt(listensock, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
	if (result < 0) {
		perror("server2");
		return 0;
	}
	
	// BINDING
	sAddr.sin_family = AF_INET;
	sAddr.sin_port = htons(1972);
	sAddr.sin_addr.s_addr = INADDR_ANY;
	result = bind(listensock, (struct sockaddr *) &sAddr, sizeof(sAddr));
	if (result < 0) 
		{
			perror("server2");
			return 0;
		}
	
	result = listen(listensock, 5);
	if (result < 0)
		{
			perror("server2");
			return 0;
		}

	// Before we start looping we install our signal handler

	signal(SIGCHLD, sigchld_handler);

	// then we call accept() and allow it to block waiting for connection requests from clients.
	// After accept returns we call fork() to create a new process. If it returns 0 that means
	// we are within the child process, otherwise (in parent processe) fork returns PID of the
	// brand new child process.

	while (1)
		{
			newsock = accept(listensock, NULL, NULL);
			if ((pid = fork()) == 0)
				{
					// this means this process is the child 
					printf("child process (PID=%i) created. \n", getpid());
					close(listensock);
					// Cerramos el sock correspondiente al parent pero en el proceso child.
					// Como estamos en el child debemos cerrar el primer sock en esta instancia.
					// Porque fork hace una copia exacta de todos los recursos de la instancia parent.
				
					nread = recv(newsock,buffer, 25, 0);
					buffer[nread] = '\0';
					printf("%s\n", buffer);
					send(newsock, buffer, nread,0);
					close(newsock);
					printf("child process (PID=%i) is finished. \n", getpid());
					exit(0);
				}
		// this line will only run in parent process. The socket will remain opened in child process.
		close(newsock);
		}
}

void sigchld_handler(int signo)
{
	while (waitpid(-1, NULL, WNOHANG) > 0);
}
Thanks!
 
Old 07-15-2012, 12:29 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 1,940

Rep: Reputation: 524Reputation: 524Reputation: 524Reputation: 524Reputation: 524Reputation: 524
For a start add error-checks after accept, recv and send
 
Old 07-15-2012, 03:24 AM   #3
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Rep: Reputation: 282Reputation: 282Reputation: 282
My socket programming is very rusty (and therefore I might be fully on the wrong track) and I don't have a linux box at hand for testing.

If I look at your code, you close the connection and exit the child when something is received. That can be either a single character or a full string of multiple of characters. But once received, it will exit the child.

My guess is that the telnet application in windows sends a single character at a time and therefor after receiving it your program will stop while the linux version will only send after you've pressed <enter>. Simplest check to see if I'm on the right track would be to print the number of characters received. Solution can be to wait for <enter> (<CR> and/or <LF>) before you close the connection and exit.

Good luck.
 
Old 07-16-2012, 08:44 AM   #4
matiasar
Member
 
Registered: Nov 2006
Location: Argentina
Distribution: Debian
Posts: 314
Blog Entries: 1

Original Poster
Rep: Reputation: 31
Thanks fellows for your help.
Wim, I think you are right. I tried again and telnet client in windows behaves different from linux version.
I tried the same socket using netcat for windows and that way works the same way as nc for linux.

I'll do some more testing and if I found something interesting I'll post again.

Regards,
Matías


Quote:
Originally Posted by Wim Sturkenboom View Post
My socket programming is very rusty (and therefore I might be fully on the wrong track) and I don't have a linux box at hand for testing.

If I look at your code, you close the connection and exit the child when something is received. That can be either a single character or a full string of multiple of characters. But once received, it will exit the child.

My guess is that the telnet application in windows sends a single character at a time and therefor after receiving it your program will stop while the linux version will only send after you've pressed <enter>. Simplest check to see if I'm on the right track would be to print the number of characters received. Solution can be to wait for <enter> (<CR> and/or <LF>) before you close the connection and exit.

Good luck.
 
Old 07-16-2012, 10:00 AM   #5
matiasar
Member
 
Registered: Nov 2006
Location: Argentina
Distribution: Debian
Posts: 314
Blog Entries: 1

Original Poster
Rep: Reputation: 31
I found windows' telnet client sends new line character after every single character.
Don't now the reason and if that behavour is configurable...
 
Old 07-16-2012, 10:21 AM   #6
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,523

Rep: Reputation: 332Reputation: 332Reputation: 332Reputation: 332
Quote:
Originally Posted by matiasar View Post
I found windows' telnet client sends new line character after every single character.
Don't now the reason and if that behavour is configurable...
The other day, while at home, I was able to duplicate the issue you witnessed. I do not have a similar setup at my office, but I was wondering if you could try something?

Launch the Windows telnet application, and then enter the command 'send'. Then try to connect your your Linux server to send data.

Code:
> telnet

Microsoft Telnet> send

Microsoft Telnet> open serverIP serverPort
P.S. The 'send' option tells Telnet to send strings.
 
Old 07-16-2012, 03:55 PM   #7
matiasar
Member
 
Registered: Nov 2006
Location: Argentina
Distribution: Debian
Posts: 314
Blog Entries: 1

Original Poster
Rep: Reputation: 31
DWhitney67, thanks! You were pretty pretty close.
Following your tip I did, with windows' telnet client:

Code:
> telnet IP_OF_SERVER_HOST PORT

telnet escape (<ctrl> })

> sen STRING_TO_SEND <enter>
Plain text string is sent ok, the only thing is missing is to send the newline (\n) character at the end of the string. Still testing to see how to send it.

Regards,
Matías
 
  


Reply

Tags
client, socket, windows


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
Client Server Socket TCP/IP program in C Linux over HTTPS fahad.anwar Linux - Newbie 6 05-29-2012 04:59 AM
Client Server Socket TCP/IP program in C Linux using threads fahad.anwar Linux - Newbie 2 05-18-2012 05:34 AM
[SOLVED] Probem Ipsec - racoon roadwarrior client - main mode - hybrid_rsa authentication popi Linux - Networking 1 06-28-2011 12:05 PM
linux server socket:client IE or firefox athena_1 Programming 3 08-17-2006 09:57 AM
cannot read data at server socket, though client socket sends it jacques83 Linux - Networking 0 11-15-2005 02:58 PM


All times are GMT -5. The time now is 11:24 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration