![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Feb 2006
Posts: 214
Rep Power: 0
![]() |
networking with sockets
I'm working on sending information to a server with my tetris application.
I have the sever and a client and in order to test the program i ran it on the same computer. Does anyone know why it won't connect? maybe a firewall problem? I put unblock to the server part of the program when "windows firewall" gave me a prompt. I'm using windows xp if that helps. import java.lang.*;
import java.io.*;
import java.net.*;
class Client {
public static void main(String args[]) {
try {
Socket skt = new Socket("localhost", 1234);
BufferedReader in = new BufferedReader(new
InputStreamReader(skt.getInputStream()));
System.out.print("Received string: '");
while (!in.ready()) {}
System.out.println(in.readLine()); // Read one line and output it
System.out.print("'\n");
in.close();
}
catch(Exception e) {
System.out.print("Whoops! It didn't work!\n");
}
}
}import java.lang.*;
import java.io.*;
import java.net.*;
class Server {
public static void main(String args[]) {
String data = "Toobie ornaught toobie";
try {
ServerSocket srvr = new ServerSocket(1234);
Socket skt = srvr.accept();
System.out.print("Server has connected!\n");
PrintWriter out = new PrintWriter(skt.getOutputStream(), true);
System.out.print("Sending string: '" + data + "'\n");
out.print(data);
out.close();
skt.close();
srvr.close();
}
catch(Exception e) {
System.out.print("Whoops! It didn't work!\n");
}
}
}
__________________
Death smiles at us all. All a man can do is smile back. |
|
|
|
|
|
#2 |
|
Expert Programmer
|
Without testing it all, I can't see too much wrong. Plus, I've never worked with sockets in java before. I'm guessing that your getting "whoops, it didn't work" error that you have there. In a help to everyone else, tell is what the exception is. Does it just say it can't connect/find server?
|
|
|
|
|
|
#3 | |
|
Hobbyist Programmer
Join Date: Feb 2006
Posts: 214
Rep Power: 0
![]() |
Quote:
__________________
Death smiles at us all. All a man can do is smile back. |
|
|
|
|
|
|
#4 | |
|
Expert Programmer
|
Quote:
try System.out.print(e.toString()); Then see what you get. Capitialization matters so you might have to change it. |
|
|
|
|
|
|
#5 |
|
Programmer
Join Date: Feb 2006
Location: Columbus, OH
Posts: 84
Rep Power: 3
![]() |
I ran your code on my machine and it works fine.
|
|
|
|
|
|
#6 | |
|
Expert Programmer
|
Quote:
|
|
|
|
|
|
|
#7 |
|
Professional Programmer
|
And in the future, stop using try catch block without printing the error , then .... well it's pointless if can't see the error.
So always use as sugested by Booooze System.out.print(e.toString());
__________________
Don't take life too seriously, it's not permanent ! |
|
|
|
|
|
#8 | |
|
Professional Programmer
Join Date: Mar 2005
Location: Student of University of Mumbai, Maharashtra State, India
Posts: 344
Rep Power: 4
![]() |
Quote:
System.out.print(e.getMessage());
__________________
Visit: http://www.somaiya.edu |
|
|
|
|
|
|
#9 |
|
Professional Programmer
|
or
e.printStackTrace();
__________________
Don't take life too seriously, it's not permanent ! |
|
|
|
|
|
#10 | |
|
Hobbyist Programmer
Join Date: Feb 2006
Posts: 214
Rep Power: 0
![]() |
Quote:
Now your probably thinking "turn off windows firewall" but that's done already. So any ideas?
__________________
Death smiles at us all. All a man can do is smile back. |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|