LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 08-06-2012, 01:49 AM   #1
gajananh999
Member
 
Registered: Aug 2012
Posts: 94

Rep: Reputation: Disabled
Socket programming question


Hello All

I m new to linux and i m using socket programming to access data from one server to another server.

both Client and server i have written in C i m sending some data to server. after some time i m getting below error.


ERROR writing to socket: Bad address


earlier my client use maximum 2MB memory and when i get this error on server my client usage may go up to 1GB i dont know wats wrong.


Please help me

Last edited by gajananh999; 08-07-2012 at 01:24 AM.
 
Old 08-06-2012, 02:27 AM   #2
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,513

Rep: Reputation: 1895Reputation: 1895Reputation: 1895Reputation: 1895Reputation: 1895Reputation: 1895Reputation: 1895Reputation: 1895Reputation: 1895Reputation: 1895Reputation: 1895
You are not serious are you? This has to be a trick question.

Quote:
"Help Me!!" or "URGENT"
To you this is urgent, but to anyone else on a list where free advice is given. Also, how does this title describe the problem in any way, shape or form?
Quote:
both Client and server i have written in C i m sending some data to server.
So where is the code?
What data?
Quote:
ERROR writing to socket: Bad address
Where are you receiving this error message? (ie. a log, on screen, etc)
As this makes no reference to a line number it leads me to believe (maybe wrongly but you have included no other detail) that this is an error catch you may
have written into the code. If so, you should know what would cause such an issue.
Quote:
earlier my client use maximum 2MB memory and when i get this error on server my client usage may go up to 1GB i dont know wats wrong.
Again without code and a lot more explanation, my pluck the answer out of the air would be memory leak.

In short, help us to help you. If you do not provide anything tangible for us to help with the question will fall by the way side as it is too hard
to find solutions for you.
 
Old 08-06-2012, 02:35 AM   #3
gajananh999
Member
 
Registered: Aug 2012
Posts: 94

Original Poster
Rep: Reputation: Disabled
Dear GURU,

Thanks for your reply

here is the server code
Code:
void handlesocket(int newsockfd)
{
	char *buffer, *temp, *temp2;
	int n,i;

	if (newsockfd < 0) 
		error("ERROR on accept");

	buffer = new char[960000];
	temp = new char[16];
	temp[0] = '\0';
	temp2 = new char[1];
	temp2[0] = '\0';
	i = 0;
	do
	{
		n = read(newsockfd,temp2, 1);	
		if(temp2[0]=='@') break;
		temp[i++] = temp2[0];
		temp[i] = '\0';			
	}
	while(n > 0);

	printf("Waiting for client -----> %s bytes\n",temp);
	n = 0;
	do
	{		
		buffer[0] = '\0';	
		//printf("hello");
		n += read(newsockfd,buffer,atoi(temp));
		buffer[n] = '\0';
		writeintofile(buffer);
		printf("-----------------\n%s-------------\n",buffer);
	}
	while(n < atoi(temp));

	printf("Recieved ------------>%d bytes\n", n);

	n = write(newsockfd,&temp,strlen(temp)-1);
	printf("Sent back ------------>%s \n", temp);

	

	if (n < 0) error("ERROR writing to socket");
	close(newsockfd);
	free(buffer);
	free(temp);
	free(temp2);	
}

int main(int argc, char *argv[])
{
	int sockfd, newsockfd, portno;
	socklen_t clilen;

	struct sockaddr_in serv_addr, cli_addr;
	//added to make use ofre address
	//setsockopt(SO_REUSEADDR)
	if (argc < 2) {
		fprintf(stderr,"ERROR, no port provided\n");
		exit(1);
	}
	sockfd = socket(AF_INET, SOCK_STREAM, 0);
	if (sockfd < 0) 
		error("ERROR opening socket");
	bzero((char *) &serv_addr, sizeof(serv_addr));
	portno = atoi(argv[1]);
	serv_addr.sin_family = AF_INET;
	serv_addr.sin_addr.s_addr = INADDR_ANY;
	serv_addr.sin_port = htons(portno);
	if (bind(sockfd, (struct sockaddr *) &serv_addr,
		sizeof(serv_addr)) < 0) 
		error("ERROR on binding");
	while(1)
	{
		listen(sockfd,5);
		clilen = sizeof(cli_addr);
		newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
		handlesocket(newsockfd);
	}
	close(sockfd);
	return 0; 
}
here is the client code

Code:

#define DEFAULT_PROTO SOCK_STREAM

char *server_name = new char[255];
char *portasstring = new char[64];
char *formatasstring = new char[64];
char *storefilename=new char[2048];

void sendline(char* data)
{
	//char *server_name= "23.23.84.118";
	unsigned short port = atoi(portasstring);
	int retval, loopflag = 0;
	int i, loopcount, maxloop=-1;
	unsigned int addr;
	int socket_type = DEFAULT_PROTO;
	struct sockaddr_in server;
	struct hostent *hp;
	char *temp;
	char *temp1=new char[48000];
	

	sprintf(temp1,"%s%s",formatasstring,data);

	sprintf(data,"%s",temp1);

	WSADATA wsaData;
	SOCKET  conn_socket;
	if ((retval = WSAStartup(0x202, &wsaData)) != 0)
	{
		fprintf(stderr,"Client: WSAStartup() failed with error %d\n", retval);
		WSACleanup();
	}
	
	addr = inet_addr(server_name);
	hp = gethostbyaddr((char *)&addr, 4, AF_INET);

	memset(&server, 0, sizeof(server));
	memcpy(&(server.sin_addr), hp->h_addr, hp->h_length);
	server.sin_family = hp->h_addrtype;
	server.sin_port = htons(port);
	
	conn_socket = socket(AF_INET, socket_type, 0); /* Open a socket */
	if (conn_socket <0 )
	{
		WSACleanup();
		return;
	}
	
	if (connect(conn_socket, (struct sockaddr*)&server, sizeof(server)) == SOCKET_ERROR)
	{
		WSACleanup();
		return;
	}
	
	temp = new char[255];
	temp[0] = '\0';
	sprintf(temp, "%d@\0", strlen(data));
	
	retval = send(conn_socket, temp, strlen(temp) , 0);
	printf("%s\n\n\n\n\n\n\n\n\n\n",temp);
	if (retval != SOCKET_ERROR)
	{
		
	}

	retval = send(conn_socket, data, strlen(data) , 0);
	//printf("%s",data);
	
	if (retval != SOCKET_ERROR)
	{
		
		temp[0] = '\0';
		retval = recv(conn_socket, temp, 255, 0);
		temp[retval+1] = '\0';
		printf("Server Received:%s\n",temp);
		
	}
	

	free(temp);
	free(temp1);
	closesocket(conn_socket);
	WSACleanup();
}

Last edited by gajananh999; 08-07-2012 at 01:22 AM. Reason: not in proper format
 
Old 08-06-2012, 09:36 AM   #4
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,513

Rep: Reputation: 1895Reputation: 1895Reputation: 1895Reputation: 1895Reputation: 1895Reputation: 1895Reputation: 1895Reputation: 1895Reputation: 1895Reputation: 1895Reputation: 1895
Well firstly, please go back to previous post and place inside [code][/code] tags to keep the formatting and make the code at least a little readable.

Secondly, you still need to help us to help you, a swath of code says that you have copied / written a lot of code.
What debugging have you done?
Have you placed print statements or other output modes to collect information surrounding the variables near where you make calls to sockets?

The general idea here is that the effort you put in will be returned.
 
Old 08-06-2012, 10:26 AM   #5
TB0ne
Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 14,592

Rep: Reputation: 2558Reputation: 2558Reputation: 2558Reputation: 2558Reputation: 2558Reputation: 2558Reputation: 2558Reputation: 2558Reputation: 2558Reputation: 2558Reputation: 2558
Quote:
Originally Posted by grail View Post
Well firstly, please go back to previous post and place inside [code][/code] tags to keep the formatting and make the code at least a little readable.

Secondly, you still need to help us to help you, a swath of code says that you have copied / written a lot of code.
What debugging have you done?
Have you placed print statements or other output modes to collect information surrounding the variables near where you make calls to sockets?

The general idea here is that the effort you put in will be returned.
Couldn't have said it better. Also gajananh999, you need to change your post subject...subjects of either "Help me" or "Urgent" are discouraged, and you've gone out of your way to do BOTH.

I'll go out on a limb and say (just from the error message), that the address is wrong/blocked, or the other side of the connection is somehow failing. Have you checked it? Firewall(s) in place? What's the network environment where you're running this?
 
Old 08-06-2012, 07:34 PM   #6
John VV
Guru
 
Registered: Aug 2005
Posts: 12,906

Rep: Reputation: 1715Reputation: 1715Reputation: 1715Reputation: 1715Reputation: 1715Reputation: 1715Reputation: 1715Reputation: 1715Reputation: 1715Reputation: 1715Reputation: 1715
you might want to REMOVE THIS
Quote:

gajanan hiroji
hgajanan@isacweb.com
+919008311701
a email AND phone number to " spam !!!! "
wow i hit the jackpot !

now i am a nice guy ... but if i was not ...............................
 
  


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
Question related to Socket programming Mushabbar Linux - Software 1 01-01-2012 10:45 PM
Linux socket programming question mjp053000 Linux - Networking 3 09-16-2010 12:31 PM
Socket programming question brianvdc Programming 2 12-30-2004 01:36 PM
Socket Programming Question? sharma_deepak83 Programming 1 06-29-2004 10:36 AM
socket programming question.. concoran Programming 4 01-14-2002 02:23 AM


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