Welcome to the most active Linux Forum on the web.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


Closed Thread
Search this Thread
Old 08-06-2012, 02:20 AM   #1
Registered: Aug 2012
Posts: 94

Rep: Reputation: Disabled
Smile ERROR writing to socket: Bad address

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


gajanan hiroji
Old 08-06-2012, 02:51 AM   #2
Registered: Jul 2012
Location: Delhi, India
Distribution: CentOS
Posts: 80

Rep: Reputation: Disabled
Would you please paste some part of client code (socket related functions only) so that we can check it.
Old 08-06-2012, 03:37 AM   #3
Registered: Aug 2012
Posts: 94

Original Poster
Rep: Reputation: Disabled
Dear Piyush,

Thanks for your reply

Server Program.

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;
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;
buffer[0] = '\0';
n += read(newsockfd,buffer,atoi(temp));
buffer[n] = '\0';
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");

client program

void sendline(char* data)
//char *server_name= "";
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];



WSADATA wsaData;
SOCKET conn_socket;
if ((retval = WSAStartup(0x202, &wsaData)) != 0)
fprintf(stderr,"Client: WSAStartup() failed with error %d\n", retval);

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 )

if (connect(conn_socket, (struct sockaddr*)&server, sizeof(server)) == SOCKET_ERROR)

temp = new char[255];
temp[0] = '\0';
sprintf(temp, "%d@\0", strlen(data));

retval = send(conn_socket, temp, strlen(temp) , 0);
if (retval != SOCKET_ERROR)


retval = send(conn_socket, data, strlen(data) , 0);

/*if (retval != SOCKET_ERROR)

temp[0] = '\0';
retval = recv(conn_socket, temp, 255, 0);
temp[retval+1] = '\0';
printf("Server Received:%s\n",temp);


Old 08-06-2012, 05:08 AM   #4
Shahid nx
Registered: Jan 2012
Posts: 41

Rep: Reputation: Disabled
n += read(newsockfd,buffer,atoi(temp));
In this statement for what purpose you have written atoi(temp) in third parameter. I think That should be the number of bytes u r attempting to read from file descripter newsockfd.
Old 08-06-2012, 07:44 PM   #5
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
Old 08-06-2012, 08:00 PM   #6
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,986
Blog Entries: 11

Rep: Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880
Please post your thread in only one forum. Posting a single thread in the most relevant forum will make it easier for members to help you and will keep the discussion in one place. This thread is being closed because it is a duplicate.

Closed Thread

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
unix-socket error:bind: Address already in use sweetytweety Linux - Newbie 2 05-12-2008 01:13 AM
recv: Bad address (UNIX Socket programming) anamericanjoe Programming 1 12-13-2006 08:25 PM
ERROR reading from socket: Bad address cynthia_thomas Programming 1 02-18-2006 07:02 AM
Socket error when spoofing source address David Knecht Linux - Networking 0 06-17-2005 05:54 AM
Socket error when spoofing source address David Knecht Suse/Novell 0 06-15-2005 07:01 AM

All times are GMT -5. The time now is 03:54 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 @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration