Jump to content
  • Advertisement
Sign in to follow this  
Mozly

Help with basic networking (TCP/IP)

This topic is 2588 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

So I'm trying to create my first multiplayer game. I want to send information through tcp/ip. Right now I just want to echo simple messages like hello.

I made a program (client and server) that can send messages in my local network but how do I get my program to do the same thing between two computers on two different networks.

From what I read, I assumed that I could just specify the ip of the server and the client could connect. When I try to run this code on another network it times out.

What do I need to add to my code in order for anyone to connect?

here is the client side app:

import java.lang.*;
import java.io.*;
import java.net.*;
import java.net.InetAddress;

//get user input
import java.util.Scanner;

public class client {

public static void main(String args[])
{
Socket sock=null;
DataInputStream dis=null;
PrintStream ps=null;

Scanner scan = new Scanner(System.in);
String message = scan.nextLine();

System.out.println("Trying to connect");

try{

InetAddress ip = InetAddress.getByName("50.20.62.8");
System.out.println(ip);

sock = new Socket(ip,4748);

//create something to print a messge to the server
ps= new PrintStream(sock.getOutputStream());
ps.println("Hi from client");
ps.println(message);

DataInputStream is = new DataInputStream(sock.getInputStream());
BufferedReader buf = new BufferedReader(new InputStreamReader(is));
System.out.println(buf.readLine());
}catch(SocketException e){
System.out.println("Socket Exception "+ e);

}catch(IOException e){
System.out.println("IOException "+e);
}

finally{
try{
sock.close();
}catch (IOException ie){
System.out.println("Close Error :" + ie.getMessage());
}
}
}
}



I should probably mention that my router has a WPA password. if that is the problem how do I circumvent that without disabling my password?

Share this post


Link to post
Share on other sites
Advertisement
It probably has nothing to do with your code, but more to do with NAT, or possibly a firewall.

1) On your router forward whatever port your server is using to your local computer's IP address
2) Make sure your router is not blocking that port with its firewall
3) Make sure your local computer is not blocking that port with its firewall
4) When you try to connect to it you need to make sure to specify your external IP address this can be obtained by going to google and typing "what is my ip"
5) If none of that works for you, you could take the less secure route and setup a DMZ on your router to point to your local computer opening it up completely to the internet.

Once all of these steps are completed and its still not working it might be your code

Share this post


Link to post
Share on other sites
I should probably mention that my router has a WPA password. if that is the problem how do I circumvent that without disabling my password?

I think you probably want to do some general reading on networking. Our recommended reading is Keshav or Kurose, of which I think I prefer the former, but these are probably more detailed than you need. Your problem here is probably NAT. Router configuration is one solution, a better (more general) one is to look into NAT punchthrough.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!