Jump to content

Sending Packets to IP


MrFish

Recommended Posts

I'm making an instant messenger program to test sending packets. I've got it so I can run the instant messenger program with any computer on my network using their network IP but when I went to try it with internet IP with someone in canada I learned that it will not connect. In this screenshot you can see the program connects fine on the left but does not connect at all on the right (the internet ip). Packets would be pretty useless if you couldn't send them over the internet so I must be doing something wrong. I'm not sure what information you would need to see so I'll post whatever I think it relevant.64975325.pngin the main method

	   				port = 1337;		connectingPort = 1337;		IP = "192.168.2.6";  //Can be changed before first message is sent							.....removed some lines				try		{			mySocket = new ServerSocket(port);			MR = new MessageReciever(mySocket);			append("Connected to port: " + port);		}		catch(IOException e)		{			append("Cannot connect to this port (" + port + ")");		}

after the send button is clicked

		public void actionPerformed(ActionEvent e)	{		if(e.getActionCommand().equals("Send Message"))		{			if(!connected)			{				IP = IPAddress.getText();				connect();			}			if(connected)			{				name = OutgoingName.getText();				outgoingMessage = name + ": " + SendMessage.getText();				SendMessage.setText("");				append(outgoingMessage);				out.println(outgoingMessage);			}		}	}

The connect method

		public void connect()	{		append("Connecting to: " + IP + " (port: " + connectingPort + ")...");		try		{			toSocket = new Socket(IP, connectingPort);			out = new PrintWriter(toSocket.getOutputStream(), true);			append("\tConnected to user.");			append("==============================================\n");			connected = true;		}		catch(IOException e)		{			append("\tCannot connect to this user.");		}	}

That's all I think it relevant. Let me know if you need more info.

Link to comment
Share on other sites

Hmmmm.. This looks like an issue you should have posted at The Java Forums, however when making socket connections remember the corresponding port has to be open on the remote machine i.e. the internet ip. The exception you caught 'e' can be printed with:

System.err.println(e);

to give more detail on the error.Also its a bit unsafe to post the ip address of a machine you are testing with.

Link to comment
Share on other sites

Well, if the target computer is behind a NAT router, then by default the router won't know which node on its local network to send the packet when it arrives. By telling the router to forward port 1337 to the target machine, the router will be able to send the packet on to the correct destination.

Link to comment
Share on other sites

Try logging into the router through your browser (default ip is usually 192.168.1.1 or 192.168.0.1) an look for a virtual server or port forwarding option. If that is not the case you may have to add an exception to your firewall or in extreme cases contact your ISP and see if they forward all ports in the first place.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...