   C# Application Communicating with C++ Application
  I am using sockets to send data between a C++ application and a C# application.  For some reason though (I'm using TCP) after the C# application begins listening for connections the C++ application cannot connect in.  I can't figure it out for the life of me!  Here's my C++ code:   WSADATA wsadata;SOCKET mySock;SOCKADDR_IN myAddr;if(WSAStartup(MAKEWORD(2, 2), &wsadata)!=0)MessageBox(NULL, "Could not initialize winsock 2.2!", "Error!", MB_OK);mySock = socket(AF_INET, SOCK_STREAM, 0);if(mySock==0)MessageBox(NULL, "Could not create a socket!", "Error!", MB_OK);memset(myAddr.sin_zero, 0, 8);myAddr.sin_addr.S_un.S_addr = INADDR_ANY;myAddr.sin_family = AF_INET;myAddr.sin_port = htons(9500);bool bMapMaking = false;DWORD dOrigTime = GetTickCount();for(;;) {if(GetTickCount()-dOrigTime>3000)break;if(connect(mySock, (const SOCKADDR *)&myAddr, sizeof(SOCKADDR))==0) {bMapMaking = true;break;}}And here is my C# code:   Socket mySock = new Socket (System.Net.Sockets.AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Stream, System.Net.Sockets.ProtocolType.Tcp);System.Net.IPEndPoint point = new IPEndPoint(IPAddress.Any, 9500);mySock.Bind(point);mySock.Listen(1);//System.Diagnostics.Process.Start("MyC++App.exe");Socket finalSock = mySock.Accept();Application.Run(new Form1());mySock.Close();finalSock.Close();Now, I have also tried passing 9500 to System.Net.IPAddress.HostToNetworkOrder before passing it into my IPEndPoint structure but the same thing happens.  When the C++ application tries to connect in it never succeeds.  After implementing WSAGetLastError() I find that connect is failing because of error number 10049.  MSDN defines this error as:    Cannot assign requested address. The requested address is not valid in its context. This normally results from an attempt to bind to an address that is not valid for the local computer. This can also result from connect, sendto, WSAConnect, WSAJoinLeaf, or WSASendTo when the remote address or port is not valid for a remote computer (for example, address or port 0).What do I do? I think its because I'm setting the remote IP to INADDR_ANY.  Is this correct?  Any help would be greatly appreciated.[edited by - 31337 on February 10, 2004 10:47:37 AM]
  Your C# code looks ok. Maybe you can try listening to 2 connections.Much simpler would be to use the tcpserver class: TcpListener newserver = new TcpListener(9050);newserver.Start();TcpClient newclient = newserver.AcceptTcpClient(); // blocks hereNetworkStream ns = newclient.GetStream();byte[] outbytes = Encoding.ASCII.GetBytes("Welcome");ns.Write(outbytes, 0, outbytes.Length);byte[] inbytes = new byte[1024];ns.Read(inbytes, 0, inbytes.Length);string instring = Encoding.ASCII.GetString(inbytes);Console.WriteLine(instring);ns.Close();newclient.Close();newserver.Stop();I noticed in your C++ you specify INADDR_ANY. I am not sure if that''s allowed for connect(), probably only for listen. If you run on the same machine, try "127.0.0.1" ;  Hmm, changing from IPADDR_ANY to "127.0.0.1" fixed it.  Why? ;  You must specify where to connect to. You can''t say ''connect to any address''. ;  My computer''s IP isn''t 127.0.0.1 ;quote:Original post by 31337  My computer''s IP isn''t 127.0.0.1   EVERY computer''s IP can be 127.0.0.1  It''s the Loopback IP.   Kami no Itte ga ore ni zettai naru! 