LinuxQuestions.org
Help answer threads with 0 replies.
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 03-17-2013, 01:47 PM   #1
Tygy
LQ Newbie
 
Registered: Mar 2013
Posts: 2

Rep: Reputation: Disabled
Socket programming


Hi,

I have a question about socket programming.
I have two pieces of code, one for client and one for server. I have send a string from client side to the server, but server cannot get the string and I don't know what is the problem. Would anyone help me.

client code
Code:
char location_req_msg[1024]; 
	memset(location_req_msg, '0',sizeof(location_req_msg));
	char* str="Hello World to binder from client !";
	strcpy(location_req_msg,str );

	printf("The string in the buffer before client send is:%s\n",location_req_msg); // and it shows correct value
	//char location_response_msg[1024];
	size_t len = 0;
	printf("client_binder_sock %d \n" , client_binder_sock);
	// also shows correct value
	
	len = send(client_binder_sock, location_req_msg, strlen(str),0);
        // also shows the correct length here
server code
Code:
connection = accept(sock, (struct sockaddr*)&binder_address, &length);
if (connection < 0) {
	perror("accept");
	exit(EXIT_FAILURE);
}
	
while(connection)
{	
	n = recv(sock, readBuffer, 1024, 0);
	   if (n <= 0)
		printf("%d",errno);
	    else {
		printf("%s", "\nsuccess in client read!\n");

       
		std::cout << "\n bytes recieved :" << n << std::endl ;
		std::cout << readBuffer << std::endl;
		memset(readBuffer, '0',sizeof(readBuffer));
	}
	connection = accept(sock, (struct sockaddr*)&binder_address, &length);
}
client or server did not show any error.

Please help me why server shows length -1 and there is not any string in this side.
 
Old 03-17-2013, 07:35 PM   #2
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,790

Rep: Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653
Have you read Beej's Guide to Network Programming ? .. you should find that most of the problems you'll encounter are described in there.
 
1 members found this post helpful.
Old 03-17-2013, 08:12 PM   #3
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
Tip: When you know that you're ing something that ten bezillion people have ed ahead of you ... such as basic socket programming ... use your head!

Get out there and "Google everything." Look for every tutorial you can think of, whether it's using exactly your target language or not.

Search this forum, too, for all the times when someone else has said ... for by learning from someone else's without blindly repeating it, you look .
 
Old 03-25-2013, 07:36 AM   #4
Tygy
LQ Newbie
 
Registered: Mar 2013
Posts: 2

Original Poster
Rep: Reputation: Disabled
Thanks.

We don't need to reply when we don't know it exactly.
I'm sorry I had a question here and got help from you anyway.
 
  


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
Lab 5: Socket Programming and Pthreads Programming teesha Linux - Newbie 2 02-27-2012 12:25 AM
Socket programming maldini1010 Programming 4 02-11-2005 11:16 PM

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

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