|
-
Jan 15th, 2007, 01:30 AM
#1
Thread Starter
Addicted Member
[RESOLVED] Having trouble testing sockets
For practice I've been working on making a very simple instant messenger program in Java. I have a server and a client made up already using port 5000. I have the client's Socket IP configuration set to the loopback interface for testing purposes. My problem is getting connected to the server... I can't! It never is able to connect. I've tried connecting across my network, and the internet... all have failed...
What am I doing wrong here?
Also, once I do get this up and running I'll have a computer with the server running constantly on it behind two routers. Let's use the IP 126.25.24.23 as an example for my IP address. In order for the client to connect to this server from an outside network, all I have to do is set the socket's IP to "126.25.24.23" then forward all requests for port 5000 to that machine, right?
-
Jan 15th, 2007, 05:22 AM
#2
Re: Having trouble testing sockets
For the connection problem, show some code.
For the other, what is you real IP address? Do any of the routers do NAT or DNAT? Is your outside IP (the IP that IP-finders report when you connect to them) the same as the IP configured on your server? You might have to set up port forwards on the routers.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jan 15th, 2007, 03:37 PM
#3
Thread Starter
Addicted Member
Re: Having trouble testing sockets
Here is the method that contains the socket for the client:
Code:
private void setUpNetworking() {
try {
sock = new Socket("127.0.0.1", 5000);
InputStreamReader streamReader = new InputStreamReader(sock.getInputStream());
reader = new BufferedReader(streamReader);
writer = new PrintWriter(sock.getOutputStream());
System.out.println("networking established");
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "IO ERROR")
}
}
And for the server,
the client handler:
Code:
public ClientHandler(Socket clientSocket) {
try {
sock = clientSocket;
InputStreamReader isReader = new InputStreamReader(sock.getInputStream());
reader = new BufferedReader(isReader);
} catch (Exception ex) {
ex.printStackTrace();
}
}
and the method containing the server socket:
Code:
public void go() {
clientOutputStreams = new ArrayList();
try {
ServerSocket serverSock = new ServerSocket(5000);
while (true) {
Socket clientSocket = serverSock.accept();
PrintWriter writer = new PrintWriter(clientSocket.getOutputStream());
clientOutputStreams.add(writer);
Thread t = new Thread(new ClientHandler(clientSocket));
t.start();
System.out.println("got a connection");
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
As for the routers,
I'll definitely need to port forward, I wasn't very clear on that in my first post. I'm not having problems with that part as I haven't gotten to it yet, I was just wondering if that's all I would need to do in order for this messenger to work correctly.
-
Jan 15th, 2007, 05:50 PM
#4
Re: Having trouble testing sockets
Do you get a specific error? Any exception stack traces?
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jan 15th, 2007, 06:18 PM
#5
Thread Starter
Addicted Member
Re: Having trouble testing sockets
Nevermind, I got it working. The problem was with another method... it had actually been connected all along, it just wasn't outputting the received messages right.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|