[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 259
  • Last Modified:

Getting Address in Use while connectihg thru Sockets

I am trying to connect using sockets. However I get EADDRINUS error.
Any idea why ???
0
sjohn
Asked:
sjohn
1 Solution
 
GunnarCommented:
This message generally occurs when you create a server socket and the specified port is occupied by a different socket. However, as I understand your question you are trying to create a client socket (in which case you do not specify a port number anyway), is this correct?
0
 
rdelfinoCommented:
I suppose you're trying to create a client application  that uses stream sockets (TCP) and I think you tryed to bind (calling bind()) the socket before trying to connect. You do not have to bind the
socket before connecting. The OS do it automatically to you
when you call connect() - except when  an error occurs.

Try this:

/* includes . . . */
int main(void)
{

int s;
int port;
struct sockaddr_in sockAddr;

/* initializing . . .*/

s = socket(AF_INET, SOCKSTREAM, 0);
if (s <0)
{
 printf("Error %d . . .\n", errno);
 exit(0);
}

memset(&sockAdd, 0, sizeof(sockAddr);
port = 2000; /* use the port you want */

sockAddr.sin_family = AF_INET;
sockAddr.sinport = port;

sockAddr.sin_addr.s_addr = inet_addr("143.107.45.230");
/* use the IP addres you want */

if (connect(s, &sockAddr, sizeof(sockAddr))<0)
{
 printf("Connect error %d . . .\n", errno);
 exit(0);
}

/* the rest of your program . . .*/

}

I hope it helps

Reginaldo

0
 
sjohnAuthor Commented:
I believe I had not been specific in defining my scenerio under which I am getting this error

I have a client using TCP based SOCK_STREAM communication. I am not calling bind and just creating the socket and calling connect(). The server is not under my control and is written by some one else.

The problem occures when I close the socket and try to connect to the same server IP address. This is when I get Address in Use, Connect failed
0
 
MoondancerCommented:
This question was awarded, but never cleared due to the JSP-500 errors of that time.  It was "stuck" against userID -1 versus the intended expert whom you awarded.  This corrects the problem and the expert will now receive these points; points verified.

Please click on your Member Profile and select "View Question History" to navigate through any open or locked questions you may have to update and finalize them.

This is the Community Support link, if help is needed, along with the link to All Topics since many new ones were recently added.

http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
http://www.experts-exchange.com/jsp/zonesAll.jsp
 
Thanks,
Moondancer
Moderator @ Experts Exchange
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now