Can´t open a socket connection from J2ME to a C# server - c#

I´m trying to open a tcp socket from a j2me midlet on a symbian device to a windows (C#) socket server. The server is working and was tested for months.
Now when I try to open a socket from the midlet
clientSocketConnection = (SocketConnection) Connector.open("socket://" + ip + ":" + port);
it just times out with a -34 error (Could not connect).
I know the phone has network capabilities and permissions as I can open such socket connections between 2 phones (emulated) using "ServerSocket" on one side and in the same machine the server is on.
Somehow the J2ME Socket is not compatible with the C# counterpart....
listenSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
Any hint on this matter would be really appreciated.
Also, should I use another type of socket on the server side? I could not find information about the AdressFamily, SocketType nor ProtocolType for the J2ME sockets.
Adding info due to the recent comments.
It´s not a firewall problem as I have no firewall and DMZ is configured for the machine ip.
The phone (emulated) has access to internet and when I open a socket from an emulator to another, both on the same machine, using my WAN PUBLIC ip on the "client" side to find the "server" side it works.

Well it´s solved now.
Really weird but binding a j2me listening port (ServerPort) on "localhost" it´s not the same as binding a .net listening port on "localhost".
I mean, when I made a "server" midlet listening on localhost the clients could connect, but when I did it on the .net server they couldn´t.
The "solution" was to hardcore the computer ip instead of using "localhost". I really do not understand why, but this worked...

Related

c# Socket.RemoteEndPoint shows different port that I'm using to connect

I've developed a basic console application that will work as server-client. My general workflow will be like, Server starts clients connect to server and once a client send something the server will receive it and send it to ALL clients. Currently I can do this with my codes but my question is when I send back I've tested Socket.RemoteEndPoint and I saw ports like 65xxx. Like my first client had 65020, second 65021 and so go on. I wonder why this happen since I connect with using 9001 port and listen to 9001 port on my computer. I've done a little bit research but couldn't find a solution to my question. Why do I get these port numbers when I use Socket.RemoteEndPoint? It's okay for me since I don't use firewall or anything while I'm testing to even if the clients connects from another ports it's fine by me as long as its works but what if I have a firewall and I open ONLY 9001 port? Will my client-server based programs work then?
So far as the server/firewall on that machine is concerned, what normally matters is the server port. You wouldn't normally apply any filtering based on the client's port, which as I said in a comment, will normally be an ephemeral port. Under most circumstances, client code will make no effort to specify the local port or IP address and will let the OS pick appropriate values.
The Socket class's LocalEndPoint and RemoteEndPoint use "local" and "remote" to mean "from the perspective of the machine on which this code is running", not "who initiated the connection vs who was connected to".
So on the server, LocalEndPoint will give you information about the server end of the connection and RemoteEndPoint will give you information about the client end of the connection. Irrespective of who initiated the connection.

.NET UDP Client use over internet not working

I have a rather complex client server app that has three separate UDP connections.
One connection (port 50001) is for control of a device.
This connection listens for connections from a remote device and when a connection is made sends data and receives data. This works fine over a LAN as well as over the internet. Of course, port forwarding is enabled on the server network. This works fine using a mobile phone hot spot as well.
Another connection is for UDP sound being sent from the client to the server (port 50002). In this case, the client just sends UDP packets to the server using the routers internet IP Address which then by port forwarding goes to the server. This works fine over the LAN, and the Internet using a mobile phone hot spot.
Now there is a third connection (port 50003) where the issue comes in.This connection is the client receiving sound from the server. This works if I am on the lan and connect direct to the server or if I specify the Internet address of my router. But it does not work with a hot spot.
So the question is, is this an issue with the Mobile provider that is just not letting the UDP packets in over port 50003? Or, am I not connecting correctly to the hotspot?
So this is how the code works: When the connection is made from the client to the server for control of the device, I get the remote IPEndpoint address and save it. Next I just start sending the UPD packets from the server to the client using this address. Again this works over the LAN direct to the client, but it does not work if I use a hot spot.
Here is the code:
Note:address contains the address of the remote client from the first connection:
private void Connect(string address, int port) //at this point the control connection is running and the address comes from there
{
IPAddress destination = IPAddress.Parse(address)
socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
endPoint = new IPEndPoint(destination, port);
}
private void SendBytes(Byte[] bytes)
{
socket.SendTo(bytes, 0 , bytes.,Length, SocketFlags.None, endPoint)
}
So this exact same code works in the direction from client to server but in that case I am sending the UDP packets to the router internet address which has port forwarding enabled. However, it does not work in the direction from server to client. The question is why not?
Is it because a port forward is not available on the hot spot?
Why is one not needed for the control (Connection one??).
I should mention that the control connection is different than both of these: It is an async UDP server.
Thanks
Your assumption that the problem is on the missing port forwarding of the hot spot is correct.
Often the IP addresses provided by the hot spot are from a private range (e.g. 192.168.x.y) and are translated with NA(P)T to the public IP address of the hot spot.
When you send an UDP message from the client to the server, the router in the hot spot keeps the information of the source and targets IP addresses and ports. When an answer is received using the same IPs and ports, this message is forwarded to the client.
But when you use a different port number, the router doesn't know to who this message should be sent.
If possible, I would suggest that the third connection is also initiated by the client or that the data is sent over one of the other two existing connections.

I can't connect to the server (C#)

I have Server and Client scripts in C# programming language. When I create server on my computer and from another computer I'm trying to connect to the server it says:
an existing connection was forcibly closed by the remote host
It's happening when I change the IP of the server to 0.0.0.0 (IPAdress.Any)
A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
It' happening when I change the IP of the server to IPv4 which is 10.0.0.6 (On the ipconfig - IPv4).
Also I turned off the Firewall, And I opened port (11000) on website of the router.
I put in the client the public IP address of the server (which is on whatismyip.com).
The program works perfectly when it is on the local network (I mean when the both computers have the same network).
Scripts:
Server code : http://pastebin.com/awsrkjk4
Client code : http://pastebin.com/2Xrm8YnA
Does anyone know how to solve it?
First of all, check to make sure your server is actively listening on the TCP port you expect. If it is a Windows machine, from the command line try:
netstat -ano | findstr ***LISTENINGPORT***
In your case I'm guessing you should be listening on 11000.
If so, on the client side, let me know exactly where your code is breaking. I'm assuming for now it's this:
sender.Connect(new IPEndPoint(IPAddress.Parse(ip), 11000));
.. and also run a WireShark capture on the client machine.
There are plenty of quick start-up guides - basically this will allow you to see the request your Client is making across the network, & see if a connection (if any) is being established on the server.
If you ARE making a connection, then it is just being closed for another reason, then you have a code issue. If NOT, then you have a network/configuration issue. This should help you make progress.

I can't get TcpClient to connect to a TcpListener on another host

I have a simple client and server application that talk to each other over TCP/IP using TcpClient and TcpListener. If I run both the server and the client on the same computer with an IP address of 192.168.10.150, they can talk to each other just fine. However, if I move the client to a different computer on my home network, it can’t connect to the server any more (which is still running on 192.168.10.150). From the client computer, I can ping 192.168.10.150. I have turned off Windows Firewall on both computers. I am using port 2244, which I don’t think is used by something else. The two computers are both connected to the same wireless router. But this line of code in the client:
TcpClient client = new TcpClient("192.168.10.150", 2244);
Throws the following exception:
A connection attempt failed because the connected party did not
properly respond after a period of time, or established connection
failed because connected host has failed to respond
192.168.10.150:2244
Both computers are running XP. Any ideas how I get these to connect?
Thanks for the responses. It seems that a couple of you are correct that the network traffic was being stopped by the router. I hooked up the two computers with a switch, and they were able to talk. Unfortunately I haven't been able to successfully configure the router to allow the traffic, but that's OK, as I can just use the switch for now.

Unable to connect to local machine IP address using internet IP address

I've spent the last month writing a multiplayer game. I have only been testing it on one machine, using 127.0.01:9051 as the IPEndPoint.
I changed the IP address to my WAN IP, configured port forwarding on my router, configured my software firewall etc... But, it doesn't connect.
I have checked if the port is open using this site. Result: the port is really open.
Also, when I check the port from that website, my server receives packets just fine; however, when I connect from my own machine... it doesn't receive anything.
I've broken everything down to the basics to make sure it wasn't a problem with my code.
This basic code does not work:
IPEndPoint iep = new IPEndPoint(IPAddress.Parse("XXX.XXX.XXX.XXX"), 9051);
TcpClient client = new TcpClient();
client.Connect(iep);
Any ideas are much appreciated.
If you are attempting to connect to the IP that is Forwarded (outsideIP) to the same machine (insideIP), it won't work. There are very few enterprise firewalls and no consumer devices (I know of) that will route a packet from the inside out and translate it back in.
[Internet] -- outsideIP[Router/Firewall]insideIP1 -- insideIP2[Computer]
In this case, packets from insideIP(X) will not be able to connect to outsideIP.
Try to see if you can telnet to your wan IP on port 9051. Routers don't really like it when you connect to their wan IP from within the network itself.
You could use Dyndns or some other service for the outside world to connect to your server and put the same name into your host file that resolves to 127.0.0.1 so that you can connect internally.
Is your client and your server (both the same machine in your failing test) using the same endpoint ip/port? You can only bind a port once at a time. Try using two ports one for server in one for server out (clinet in). That way they will not clash. Use Telnet to check you can connect to the IP/Ports you are using and Netstat to check they are not in use already.

Categories

Resources