LinuxQuestions.org
Visit the LQ Articles and Editorials section
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
 
LinkBack Search this Thread
Old 08-25-2009, 09:11 AM   #1
sam1201
LQ Newbie
 
Registered: Jul 2009
Posts: 8

Rep: Reputation: 0
Socket programming problem.


first, thank for looking my thread.

i have some basic problem about my socket programming, but i cannot figure out the solution.

i have abbreviate the code, and make it easier to read.



server code:

socket()...
bind()...
listen()...
accept()...

while(1)
{
printf("Please wait ur friends is typing message...\n");
bzero(buffer,256);
n = read(newsockfd,buffer,255);
if (n < 0) error("ERROR reading from socket");
printf("Here is the message: %s\n",buffer);
printf("Your turn to speak: ");
bzero(buffer,256);
fgets(buffer,255,stdin);
n = write(newsockfd,buffer,strlen(buffer));
if (n < 0) error("ERROR writing to socket");
}

--------------------------------------------

client code:

socket()...
connect()...

while(1)
{
printf("Your turn to speak: ");
bzero(buffer,256);
fgets(buffer,255,stdin);
n = write(sockfd,buffer,strlen(buffer));
if (n < 0)
error("ERROR writing to socket");
bzero(buffer,256);
printf("Please wait ur friends is typing message...\n");
n = read(sockfd,buffer,255);
if (n < 0)
error("ERROR reading from socket");
printf("%s\n",buffer);
}

problem description:
my problem is the display of this statement -- printf("Please wait ur friends is typing message...\n");

it cannot display correctly. let me briefly descript what happen to the output:

first i run server program, it run and waiting...
second i run the client, it ask the user to key in message...

but the problem is in the first step, the message "Please wait ur friends is typing message...\n" is not displayed immediately after the client connect to server. it is only displayed when the client send the first message to server. and so the message "Please wait ur friends is typing message...\n" always display with the opponent message together, that is not what i want. my objective is display this message and inform the user to wait for the opponent to type the message.
----------------------------------------------

from the problem above i found that i really not understand the mechanism of socket.

1) when i run the server program, where it hang for the client to connect?listen() or accept()?

2) when i put read() in server program, and write() in client() program, after that i run the server program, whould the read() that in the server program wait for the write() that in the client program?

the machanism behind is really hard to understand, most of the book only teach me how to use the function only, like socket() listen()...and what arguments should we give to them only.

really appreciated.
 
Old 08-25-2009, 09:24 AM   #2
orgcandman
Member
 
Registered: May 2002
Location: dracut MA
Distribution: Ubuntu; PNE-LE; LFS (no book)
Posts: 594

Rep: Reputation: 102Reputation: 102
For the kind of thing you're trying to do (I'm assuming making something like the AIM/YIM 'keyboard animation while typing') you'll need to do a lot more than just call the socket operations. It'll require a protocol.

An example of a crappy protocol:

Quote:
HELLO <name>

DATA <text>

BEGIN TYPING
END TYPING

BYE
Now, when you connect, you must always send a HELLO message, followed by a name given by the user. When you determine that the user is typing, you send a BEGIN TYPING message. When they've stopped typing, you send an END TYPING message. When they send some message, you send a DATA message. When the person is going to gracefully shutdown (don't know when users are particularly graceful) you can send a BYE message to release resources.

This will require the use of select(), and a "open-loop" which continually monitors the input descriptor, and the network descriptor for any state changes, and will let you send appropriate messages.

BTW, this is just an example. You'll have to probably get more detailed and verbose with any protocol you develop on your own.
 
Old 08-25-2009, 10:16 PM   #3
sam1201
LQ Newbie
 
Registered: Jul 2009
Posts: 8

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by orgcandman View Post
For the kind of thing you're trying to do (I'm assuming making something like the AIM/YIM 'keyboard animation while typing') you'll need to do a lot more than just call the socket operations. It'll require a protocol.

An example of a crappy protocol:



Now, when you connect, you must always send a HELLO message, followed by a name given by the user. When you determine that the user is typing, you send a BEGIN TYPING message. When they've stopped typing, you send an END TYPING message. When they send some message, you send a DATA message. When the person is going to gracefully shutdown (don't know when users are particularly graceful) you can send a BYE message to release resources.

This will require the use of select(), and a "open-loop" which continually monitors the input descriptor, and the network descriptor for any state changes, and will let you send appropriate messages.

BTW, this is just an example. You'll have to probably get more detailed and verbose with any protocol you develop on your own.
how about the read() and write () ?? hopefully, u can answer me the mechanism inside...

i only learn how to use those socket function, but the lecturer want me to implement a interaction between client and server, sending the message to manipulate the book database...

i dont think i can achieve it...

actually most of ur reply i cannot understand... haha, anyway, thanks.
 
Old 08-25-2009, 11:38 PM   #4
orgcandman
Member
 
Registered: May 2002
Location: dracut MA
Distribution: Ubuntu; PNE-LE; LFS (no book)
Posts: 594

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by sam1201 View Post
how about the read() and write () ?? hopefully, u can answer me the mechanism inside...

i only learn how to use those socket function, but the lecturer want me to implement a interaction between client and server, sending the message to manipulate the book database...

i dont think i can achieve it...

actually most of ur reply i cannot understand... haha, anyway, thanks.
read() and write()? I don't understand the question? When you write data it is sent to the underlying driver for the file descriptor. When you read (in blocking mode) the system puts you to sleep waiting for data (unless there's already data available). Once data is available you are woken up and are given the data. You can't use read/write to determine when the end user has sent or will be sending data.

I think you need to read your class notes .
 
Old 08-26-2009, 12:40 AM   #5
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi, sam1201 -

Orgcandman is absolutely correct:

1. You'll often want to read keyboard input *and* network input in the *same* "while" loop. This isn't the only alternative - but it's a very common idiom. And the way one usually makes it work is to call "select()" ... which waits for EITHER the next network input *OR* the next keystroke.

2. A socket "recv()" or "recvfrom()" requires that you KNOW IN ADVANCE exactly how many bytes you're going to receive. I won't bore you with the "why" (it's easy to Google for), but this means either:

a) you somehow need to communicate to the client exactly how many bytes are going to be sent
... OR ...
b) the client knows he's "done" when he gets some kind of "delimiter": such as the "\n" (newline) character!

STRONG SUGGESTION:

Please read through "Beej's Guide" (if you're not already familiar with it). It's an EXCELLENT resource; it does a good job of explaining many of the "WHY'S" of sockets programming - not just the "WHAT's":

http://beej.us/guide/bgnet/

'Hope that helps .. PSM
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
I/O problem in socket programming goldeneagle1234 Programming 2 06-19-2009 12:06 PM
I/O problem in socket programming goldeneagle1234 Linux - Networking 1 06-19-2009 11:24 AM
socket programming problem muskvar Programming 14 10-16-2007 09:35 AM
socket programming problem...??? arunka Programming 4 10-11-2005 08:08 AM
socket programming problem bgraur Programming 2 03-09-2003 09:05 AM


All times are GMT -5. The time now is 07:44 AM.

Main Menu
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