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 |
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.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
04-22-2008, 04:42 PM
|
#1
|
|
Member
Registered: Mar 2008
Location: Arg
Distribution: Cent0S
Posts: 48
Rep:
|
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.
|
|
|
|
04-22-2008, 04:47 PM
|
#2
|
|
Member
Registered: Apr 2008
Location: Gliwice, Poland
Distribution: Any. BSD most often ;-)
Posts: 31
Rep:
|
Have you checked Beej's Guide to Network Programming?
http://beej.us/guide/bgnet/
Cheers,
lukost
|
|
|
|
04-22-2008, 05:37 PM
|
#3
|
|
LQ 5k Club
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,257
|
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.
|
|
|
|
04-22-2008, 10:23 PM
|
#4
|
|
Senior Member
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 2,962
Rep: 
|
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
|
|
|
|
04-23-2008, 09:24 AM
|
#5
|
|
Member
Registered: Mar 2008
Location: Arg
Distribution: Cent0S
Posts: 48
Original Poster
Rep:
|
[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);
|
|
|
|
04-23-2008, 11:32 AM
|
#6
|
|
Senior Member
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 2,962
Rep: 
|
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.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 03:47 PM.
|
|
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
|
|