Answered by:
Why TCP socket client can not connect to socket server

Question
-
I am testing a TCP socket app in wifi-environment.
1.) I am using VS2005 to create Console App for Socket-Server (Netframework v2.0)
1.1) : Launch the Console App with an IPAddress : 192.168.1.100 and portNo 3456
2. Using Windows phone as Client to connect the Socket-server .2.0 ) set Up wifi connection in the phone.
2.1) Start the App to connect the server in Wifi
the Client code to connect the server ( No compiled error )
//-- taking idea from :
http://msdn.microsoft.com/en-us/library/windowsphone/develop/hh202858(v=vs.105).aspx
// create endpoint
var ipAddress = IPAddress.Parse(hostName);
var endpoint = new IPEndPoint(ipAddress, portNumber); // ( 192.168.1.100 , 3456)// Create a stream-based, TCP socket using the InterNetwork Address Family.
_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
SocketAsyncEventArgs socketEventArg = new SocketAsyncEventArgs();
socketEventArg.RemoteEndPoint = endpoint;...... to do task here
3. Result show : No Connectiona. can Socket work for windows phone in this manner ? Is there anything I miss out?
However, it work in WP emulator. Appreciate your help.
Thursday, February 21, 2013 5:34 AM
Answers
-
Hi,
FYI the example you are using uses System.Net.Sockets which works for 7.1 or 8.0 apps. If writing an 8.0 app, you can use Windows.Networking.Sockets; which is WinRT shared by Windows Store apps. The latter is preferred.
If it works with the emulator but not the physical device the issue is likely due to the configuration of the shared WiFi environment (of course make sure your phone is connected to the desired WiFi network). All devices must be on the same local network, and the WiFi must support DHCP.
The trick on the client side is identifying the IP address of the server side device. One way to do this is to have the client send a multicast datagram out, which all devices can respond to by replying with a unicast datagram; so you can collect their IP addresses.
Hope this helps,
MarkMark Chamberlain Sr. Escalation Engineer | Microsoft Developer Support | Windows Phone 8
- Proposed as answer by Mark Chamberlain - MSFT Thursday, February 21, 2013 6:44 PM
- Marked as answer by MachineTalk Saturday, February 23, 2013 11:12 PM
Thursday, February 21, 2013 6:39 PM -
I would like to explain it with an analogy, like conversation between two people, say between you and me.
- Here you are the server and me the client. You are blind folded, so you can't see and only can 'listen'.
- I make a request to you, "May I ask you something?". You say "Yes". Thus the connection is established.
- Then you know the conversation goes on.
- Then I ask something. You are kind enough to answer it.
- But as you are speaking, I just get out, without telling you. Since you are blind folded you didn't know that I wasn't listening to you any more. That is you didn't know that I 'closed' the connection.
- After you finished answering there can be two situations: (a) If your nature is to expect some time of response from me, you will wait for it to come from me, like you may expect a 'Thank you' from me or a further question from me. So if I don't acknowledge within a given time you may set in your mind that you need not worry about me and 'close' the connection from your side. (b) You can also close the connection, immediately after answering me so that if I want to ask something more, I have to ask you again, "May I ask you something?".
So in your case, after the server sent the info requested by the client, it should expect acknowledgement from the client. If you don't get the ack from client within a given time, the server can infer the connection is dead and release the resources allocated to it by closing it.
Normally servers are not expected to bother about clients connections except to know whether to release the resources allocated. But if you have a dedicated client-server system you can do what you are thinking. Then either a server or client can keep pinging each other.
In WP7 the sockets were not allowed to be listeners. But I think in WP8 that is allowed. Pros have to confirm this as I have not yet explored it.
ThanQ...
- Marked as answer by MachineTalk Saturday, February 23, 2013 11:12 PM
Saturday, February 23, 2013 3:21 PM
All replies
-
Hi,
FYI the example you are using uses System.Net.Sockets which works for 7.1 or 8.0 apps. If writing an 8.0 app, you can use Windows.Networking.Sockets; which is WinRT shared by Windows Store apps. The latter is preferred.
If it works with the emulator but not the physical device the issue is likely due to the configuration of the shared WiFi environment (of course make sure your phone is connected to the desired WiFi network). All devices must be on the same local network, and the WiFi must support DHCP.
The trick on the client side is identifying the IP address of the server side device. One way to do this is to have the client send a multicast datagram out, which all devices can respond to by replying with a unicast datagram; so you can collect their IP addresses.
Hope this helps,
MarkMark Chamberlain Sr. Escalation Engineer | Microsoft Developer Support | Windows Phone 8
- Proposed as answer by Mark Chamberlain - MSFT Thursday, February 21, 2013 6:44 PM
- Marked as answer by MachineTalk Saturday, February 23, 2013 11:12 PM
Thursday, February 21, 2013 6:39 PM -
Hi,
Thank for the quick reply. I am trying to understand the problem better so I have some questions to ask to troubleshoot.
1) When winForm/console app Socket-Server in PC is launched
firewall dialogbox popUp
Allow vshost.exe to communicate
1) private network such as Home
2) Public network such as those in Airport , cafe( not safe)
Q1. I am not using well-known ports. I am using port 3456 why it pop Up to ask for selection?
2) Socket app on Windows Phone:when this socket app Close or Shutdown socket
- Does it send anything or data or code to the socket-server over the PC ?
What kind of data and what is the byte size? and How Socket-Server know the socket-client is closed?Thanks
Friday, February 22, 2013 5:59 AM -
My observations:
- You are using port 3456. I think this is a reserved one. For casual testing or if you are not authorized to use the reserved ports, then you should use a number above 50000 or so. Other pros may confirm if this limit number is correct. It may be different.
- When a socket closed, it just goes down. The server will be just listening and only responds to requests. If it has already got connected with the WP client and in the process of data transfer, the operation will just fail. It is up to the server code on how to handle this error.
- Coming to your main issue of 'No connection': (a) If you are using WP8 only code, then check if your WiFi adapter is configured properly in the Virtual Switch Manager accessed from the Hyper-V Manager. I am seeing this switch doesn't work properly. Sometime I have to reboot my machine when I am switching from work wifi to home wifi or from wired to wireless. (b) For WP7.1 code, mostly I had issue with the PC Server's IP Address. It is different for wired (USB) and wireless connections. Also you router may be alloting different IP for connections at different times. I had set a fixed IP to my PC in the router so that I don't have to keep changing it in my code while testing. In this respect I suppose you should consider Mark's advise. I have not tried that but I see its utility.
ThanQ...
Friday, February 22, 2013 2:42 PM -
Hi sm_on_live,
Thank you for the info.
I hope you can provide some help on this :
2.When a socket closed, it just goes down. The server will be just listening and only responds to requests. If it has already got connected with the WP client and in the process of data transfer, the operation will just fail. It is up to the server code on how to handle this error.
I like to know :
a) How Socket Server (in PC that listen to request) detects when Socket-Client inside WP app has closed like
socket.close() or User press BackKey button on MainPage or Center Microsoft button or Search button on WP?
I wan to create a interactive socket-app, i need to know:
When Socket.close()
will it send any code to Socket Server so it can detect this WP client has not connected.
Thanks.
Saturday, February 23, 2013 1:37 AM -
I would like to explain it with an analogy, like conversation between two people, say between you and me.
- Here you are the server and me the client. You are blind folded, so you can't see and only can 'listen'.
- I make a request to you, "May I ask you something?". You say "Yes". Thus the connection is established.
- Then you know the conversation goes on.
- Then I ask something. You are kind enough to answer it.
- But as you are speaking, I just get out, without telling you. Since you are blind folded you didn't know that I wasn't listening to you any more. That is you didn't know that I 'closed' the connection.
- After you finished answering there can be two situations: (a) If your nature is to expect some time of response from me, you will wait for it to come from me, like you may expect a 'Thank you' from me or a further question from me. So if I don't acknowledge within a given time you may set in your mind that you need not worry about me and 'close' the connection from your side. (b) You can also close the connection, immediately after answering me so that if I want to ask something more, I have to ask you again, "May I ask you something?".
So in your case, after the server sent the info requested by the client, it should expect acknowledgement from the client. If you don't get the ack from client within a given time, the server can infer the connection is dead and release the resources allocated to it by closing it.
Normally servers are not expected to bother about clients connections except to know whether to release the resources allocated. But if you have a dedicated client-server system you can do what you are thinking. Then either a server or client can keep pinging each other.
In WP7 the sockets were not allowed to be listeners. But I think in WP8 that is allowed. Pros have to confirm this as I have not yet explored it.
ThanQ...
- Marked as answer by MachineTalk Saturday, February 23, 2013 11:12 PM
Saturday, February 23, 2013 3:21 PM -
Hi all,
Thank you both. Now It is working. It was a silly mistake that I overlooked the wifi connection setup.
Saturday, February 23, 2013 11:17 PM -
FYI here is a new post you may find useful:
Datagram Socket Communication in Windows Phone 8
-Mark
Mark Chamberlain Sr. Escalation Engineer | Microsoft Developer Support | Windows Phone 8
Thursday, March 21, 2013 10:01 PM