LinuxAnswers - the LQ Linux tutorial section.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 03-17-2013, 02:47 PM   #1
LQ Newbie
Registered: Mar 2013
Posts: 2

Rep: Reputation: Disabled
Socket programming


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
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
connection = accept(sock, (struct sockaddr*)&binder_address, &length);
if (connection < 0) {
	n = recv(sock, readBuffer, 1024, 0);
	   if (n <= 0)
	    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, 08:35 PM   #2
Senior Member
Registered: Aug 2009
Posts: 3,790

Rep: Reputation: 650Reputation: 650Reputation: 650Reputation: 650Reputation: 650Reputation: 650
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, 09:12 PM   #3
LQ Guru
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 5,979

Rep: Reputation: 1441Reputation: 1441Reputation: 1441Reputation: 1441Reputation: 1441Reputation: 1441Reputation: 1441Reputation: 1441Reputation: 1441Reputation: 1441
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, 08:36 AM   #4
LQ Newbie
Registered: Mar 2013
Posts: 2

Original Poster
Rep: Reputation: Disabled

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.


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

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

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration