LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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-28-2008, 09:57 AM   #1
FaresH
LQ Newbie
 
Registered: May 2007
Posts: 8

Rep: Reputation: 0
Server/Client UDP


Hi,

We have a exercise to hand over in the few days to come. I tried to do it, I done most of the job but I am kind of stuck.

ftclient <host> <port> <filename> <save as>
ftserver <port> <size of each package> <delay between sent package>

The client sends a filename to the server.
If the server has the file, it sends 4 bytes to the client, which are size of the file (Using the stat() function on the server side). Else it sends one byte of 0xff, and the client closes the socket and unlink the file.
If the file exist, the client gets ready to receive the packages.
If in 10 seconds no pack arrives (I used select() ), the client aborts.
If a package arrives, it has 1 byte of success, 4 bytes of offset, and the rest is the content of the message.

What I am not able to understand is how the client receives the message in a buffer full of bytes and translate it. I know I must use the ntohl (network to host long) function. Both sides use the same architectures.

These are my question:

What is the size I should use for the buffer ?
How to copy the content of the message into another char * [] ?
How to translate from the buffer full of bytes into something I can use ?
How do I use htonl and ntohl ? I need to convert network bytes and write them to a file, how ?

Any code example would more then appreciated...

This is what I wrote in the core so far:

Loop in main

Code:
// Get ready to receive the content of the file
while(received < fileSize ){
	// Wait 10 seconds for the next package from the server
	if( select(sd+1, &readfd, NULL, NULL, &timeout) != 0 ){
		if( read(sd, &buffer, sizeof(buffer)) < 0 ){
			perror("Error in reading the package\n")
			exit(1);
		}
		// If first byte is -1 abort
		if( atoi(ntohl(buffer[0]) != -1 ){
			perror("Error sent from server\n");
			exit(1);
		}
		offset = getOffset(buffer);
		lseek(fd, offset, SEEK_SET);
		copyMsg(buffer, &temp);
		write(fd, temp, sizeof(temp), 0);
	 }else{
	        perror("Timeout\n");
	        exit(1);
        }
}
getOffset function

Code:
int getOffset( char * buffer[] ){
	static int place=1, i=1, size=0 ;
	if( i == 4 ){
		return(place);
	}
	if( (size = atoi(ntohl(buffer[i]))) > 0 ){
		place *=  size ;
	}
	i++;
	return(getOffset(buffer));
}

Last edited by FaresH; 11-28-2008 at 10:00 AM.
 
Old 11-28-2008, 03:46 PM   #2
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
Your ntohl() call is incorrect. You pass one byte to it (char), when there should be uint32_t. In fact, you should pass the complete 4 byte value, not byte by byte (ntohl changes the order of bytes, not bits).

Example:
Code:
uint32_t orig = 0;
uint32_t result = 0;
unsigned char byte0 = (unsigned char) buffer[0];
unsigned char byte1 = (unsigned char) buffer[1];
unsigned char byte2 = (unsigned char) buffer[2];
unsigned char byte3 = (unsigned char) buffer[3];
orig = (byte0 << 24) | (byte1 << 16) | (byte2 << 8) | byte3;
result = ntohl(orig);
This snipped is rather long to show how it works. You may want to print the intermediate results to see the details.

The other side should perform a similar code.

Edit: I'm assuming the data is binary, not as a string!
 
Old 11-30-2008, 05:21 AM   #3
FaresH
LQ Newbie
 
Registered: May 2007
Posts: 8

Original Poster
Rep: Reputation: 0
I got the client and server to communicate.

An integer is 4 bytes, a char is 1 byte.
I need to know how to convert the integer ( which represents the file size ) into char array of 4 cells...
 
Old 12-02-2008, 05:52 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
http://irc.essex.ac.uk/www.iota-six....ntf_sscanf.asp
 
  


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
UDP server reply to client refused johnn Programming 3 09-19-2008 06:04 AM
client/server echo program of UDP sockets memisbah Programming 2 04-08-2008 10:27 AM
simple udp server client programm in not working between two systems gssmsc Programming 4 02-26-2008 10:56 AM
how to get client ip address at udp server cranium2004 Programming 2 03-21-2005 11:35 PM
Server Client program using UDP frostmagic Programming 9 11-14-2003 11:06 PM

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

All times are GMT -5. The time now is 06:09 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