- Home /
The question is answered, right answer was accepted
Need help - Socket Server always connects to port 80
Hi All,
I am trying to create Socket Server using below code
Update:
// Server
void SetupServer()
{
try
{
IPEndPoint ipEndPoint = new IPEndPoint (IPAddress.Parse("192.168.0.2"), 9093);
Socket _serverSocket = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
_serverSocket.Bind (ipEndPoint);
_serverSocket.Listen (100);
_serverSocket.BeginAccept (new AsyncCallback (AcceptCallback), null);
}
catch (Exception ex)
{
Debug.Log (ex.Message());
}
}
void AcceptCallback(IAsyncResult ar)
{
Socket clientSocket = _serverSocket.EndAccept(ar);
_serverSocket.BeginAccept(new AsyncCallback(AcceptCallback), null);
Debug.Log("Client { " + clientSocket.GetHashCode() + " } Connected...");
clientSocket.BeginReceive(_buffer, 0, _buffer.Length, SocketFlags.None, new AsyncCallback(RecieveCallback), clientSocket);
}
// Client
void SetupClient()
{
try
{
IPEndPoint remoteEndPoint = new IPEndPoint (IPAddress.Parse("192.168.0.2"), 9093);
Socket _clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
_clientSocket.BeginConnect(remoteEndPoint, new AsyncCallback(ConnectCallback), _clientSocket);
}
catch (Exception ex)
{
Debug.Log (ex.Message());
}
}
void ConnectCallback(IAsyncResult ar)
{
Socket listener = (Socket)ar.AsyncState;
Socket handler = listener.EndConnect(ar);
Debug.Log("connected to " + listener.RemoteEndPoint.ToString());
}
But when i export the build as stand-alone exe and run it on windows machine, it always connects to 192.168.0.2:80 port.
It should connect to port 9093.
Whats wrong i am doing?
Any help would be appreciated.
When I use your code, I create a server which listens on Port 9093. I'm missing you client code though. I imagine you mean you client is connecting to your server on a wrong port? Please specify!
Can you please post you client code?
Other than that, I can only speculate it's what Harshad$$anonymous$$ said: your firewall may block you port and redirect.
Works for me, although I have to say I only tested the code without unity in a console application (due to tim reasons)...
Are you sure, this line works? Socket handler = listener.EndConnect(ar);
It does not return a Socket for me.
Answer by HarshadK · Nov 18, 2014 at 12:49 PM
One probability is that your port 9093 is blocked by your firewall hence it connects to port 80 because it is available?
Answer by chetan-rane · Nov 19, 2014 at 09:39 AM
:D HarshadK.......@Structed: the code is working at my end too...i was digging at wrong place (in my code for 80 port connection) ...the issue was with firewall :) ....anyway thanks for your help....the q's is close we can say :D
Programmer life: if you close one prob...next two are waiting for you....now some brainstroming needed to handle multiple concurrent req...
I'm glad it worked and you figured out what the problem was. Good luck with the new problem ;-)
However, the answer of @Harshad$$anonymous$$ should be promoted to an answer and accepted to give credit where credit is due ;-)
Yes Credit goes to Harshad$$anonymous$$....the issue was with firwall....thanks a tone and your ans is accepted
Follow this Question
Related Questions
Socket connection on Android/IOS/Windows 1 Answer
Unity C# Sockets? 2 Answers
WebGL sockets fails: Attempt to send to not connected connection 0 Answers
function OnCollosionEnter problems 1 Answer
Fade in Fade out in unity 1 Answer