LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
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 04-22-2008, 04:42 PM   #1
lrios
Member
 
Registered: Mar 2008
Location: Arg
Distribution: Cent0S
Posts: 48

Rep: Reputation: 15
Arrow recv() and send() in socket


Hello,
I am doing an simple chat server-client tcp and i want to pass messages between them, with recv(), and send() functions , but I dont work at all.


CLIENT
Code:
char *buffer[100];
puts("write the nick(se requeriran 3 pasos antes de ingresar)"); fgets(users[n_user].username, sizeof(users[n_user].username), stdin);	
*buffer = (users[n_user].username);
send(fdsocket, buffer, strlen(buffer), 0);
printf("Sending nick to server . . . %s\n", *buffer);

SERVER
Code:
char *buffer_c[100];
recv(fdsocket, buffer_c, MAXL_NICK, 0);
printf("Se conecto al chat -%s-\n", *buffer_c);
The server received garbage.
 
Old 04-22-2008, 04:47 PM   #2
lukost
Member
 
Registered: Apr 2008
Location: Gliwice, Poland
Distribution: Any. BSD most often ;-)
Posts: 31

Rep: Reputation: 15
Have you checked Beej's Guide to Network Programming?
http://beej.us/guide/bgnet/

Cheers,
lukost
 
Old 04-22-2008, 05:37 PM   #3
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,395
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
The line
Code:
*buffer = (users[n_user].username);
looks suspicious to me. The assignment should move only one pointer value (some assumptions here, due to lack of code) into a character buffer. The character buffer will contain garbage and no meaningful string or string terminator.
Perhaps you should be using
Code:
strcpy( buffer, users[n_user].username);
or some other function that moves bytes.
--- rod.
 
Old 04-22-2008, 10:23 PM   #4
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
How big is users[n_user].username, anyway? Is it statically sized (sizeof implies "yes")? Any reason why you don't just send using that array?
ta0kira
 
Old 04-23-2008, 09:24 AM   #5
lrios
Member
 
Registered: Mar 2008
Location: Arg
Distribution: Cent0S
Posts: 48

Original Poster
Rep: Reputation: 15
[HTML]How big is users[n_user].username, anyway? Is it statically sized (sizeof implies "yes")? Any reason why you don't just send using that array?[/HTML]

ta0kira
This is my structure <chat.h>

int n_user;
struct struc_user {

int *number_user;
char username[100];

} users[100];


So ... this way is ok ? ?

client
Code:
fgets(users[n_user].username, sizeof(users[n_user].username), stdin);
strcpy( buffer, users[n_user].username);
send(fdsocket, buffer, strlen(buffer), 0);
printf("Sending nick to server . . . %s\n", *buffer);
server
Code:
char *buffer_c[100];
recv(fdsocket, buffer_c, MAXL_NICK, 0);
printf("Se conecto al chat -%s-\n", *buffer_c);
 
Old 04-23-2008, 11:32 AM   #6
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
The buffers shouldn't be char*[], just char[]. I'd do this:
Code:
char buffer[100];
puts("write the nick(se requeriran 3 pasos antes de ingresar)");
if (fgets(users[n_user].username, sizeof(users[n_user].username), stdin))
{
send(fdsocket, users[n_user].username, strlen(users[n_user].username), 0);
printf("Sending nick to server . . . %s\n", *buffer);
}
If you really do need to copy to the buffer, I'd do it like this:
Code:
strncpy( buffer, users[n_user].username,
  (sizeof(buffer) < sizeof(users[n_user].username))?
   sizeof(buffer) : sizeof(users[n_user].username) );
ta0kira

Last edited by ta0kira; 04-23-2008 at 11:35 AM.
 
  


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
Simultaneous Socket Send/Recv in Perl? juanbobo Programming 10 07-28-2008 03:30 AM
problems in multithreaded send()/recv() using tcp socket arslanali Programming 2 05-26-2007 09:00 AM
recv() is not coming out of blocking after closing the socket. arunka Linux - Networking 2 06-30-2006 09:52 PM
recv() function of SOCKET ashucool83 Programming 1 10-08-2005 07:38 PM
function recv() in socket husniteja Programming 1 08-18-2004 09:06 AM


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