Problem connection client to server (Unknown Source)
Greetings EveryOne :D
I want to achieve server/client communication, I started with Writing the Server Side of a Socket (The Java™ Tutorials > Custom Networking > All About Sockets).
I have made some changes on the code to make server/client connect on the network not only on localhost, but unfortunately this is what i get instead:
Code:
Exception in thread "main" java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at sun.nio.cs.StreamDecoder.readBytes(Unknown Source)
at sun.nio.cs.StreamDecoder.implRead(Unknown Source)
at sun.nio.cs.StreamDecoder.read(Unknown Source)
at java.io.InputStreamReader.read(Unknown Source)
at java.io.BufferedReader.fill(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at KnockKnockServer.main(knockknockserver.java:50)
This is the code i'm working on:
Code:
import java.net.*;
import java.io.*;
public class KnockKnockServer
{
public static void main(String args[]) throws IOException
{
int Port = Integer.parseInt(args[0]); //4444;
ServerSocket S_Socket = null;
try
{
S_Socket = new ServerSocket(Port);
}
catch(IOException IOE)
{
System.err.println("Could Not Listen On Port: "+Port+".");
System.exit(-1);
}
Socket C_Socket = null;
try
{
C_Socket = S_Socket.accept();
}
catch(IOException IOE)
{
System.err.println("Accept Failed.");
System.exit(-1);
}
PrintWriter PW = new PrintWriter(C_Socket.getOutputStream(), true);
BufferedReader BR = new BufferedReader(new InputStreamReader
(C_Socket.getInputStream()));
String InputLine, OutputLine;
KnockKnockProtocol KKP = new KnockKnockProtocol();
OutputLine = KKP.ProcessInput(null);
PW.println(OutputLine);
while((InputLine = BR.readLine()) != null)
{
OutputLine = KKP.ProcessInput(InputLine);
PW.println(OutputLine);
if(OutputLine.equalsIgnoreCase("Bye."))
{
System.out.println("Bye Bye.");
break;
}
}
PW.close();
BR.close();
C_Socket.close();
S_Socket.close();
}
}
Code:
import java.io.*;
import java.net.*;
public class KnockKnockClient
{
public static void main(String args[]) throws IOException
{
String IP$ = args[0];
int Port = Integer.parseInt(args[1]); //4444;
Socket C_Socket = null;
PrintWriter PW = null;
BufferedReader BR = null;
try
{
// C_Socket = new Socket("localhost", Port);
C_Socket = new Socket(InetAddress.getByName(IP$), Port);
PW = new PrintWriter(C_Socket.getOutputStream(), true);
BR = new BufferedReader(new InputStreamReader
(C_Socket.getInputStream()));
}
catch(UnknownHostException UHE)
{
System.err.println("Don't Know About Host: "+IP$);
System.err.println(UHE);
System.exit(-1);
}
catch(IOException IOE)
{
System.err.println("Couldn't Get I/O For The Connection To: "
+IP$);
System.err.println(IOE);
System.exit(-1);
}
BufferedReader StdIn = new BufferedReader(new InputStreamReader
(System.in));
String FromServer;
String FromUser;
while((FromServer = BR.readLine()) != null)
{
System.out.println("Server: "+FromServer);
if(FromServer.equals("Bye."))
break;
FromUser = StdIn.readLine();
if(FromUser != null)
{
System.out.println("Client: "+FromUser);
PW.println(FromUser);
}
}
PW.close();
BR.close();
StdIn.close();
C_Socket.close();
}
}
Code:
import java.net.*;
import java.io.*;
public class KnockKnockProtocol
{
private static final int WAITING = 0;
private static final int SENTKNOCKKNOCK = 1;
private static final int SENTCLUE = 2;
private static final int ANOTHER = 3;
private static final int NUMJOKES = 5;
private int State = WAITING;
private int CurrentJoke = 0;
private String Clues[] =
{
"Turpin", "Little Old Lady", "Atch", "Who", "Who"
};
private String Answers[] =
{
"Turpin The Heat, It's Cold In Here!",
"I Didn't Know You Could Yodel!",
"Bless You!",
"Is There An Owl In Here?",
"Is There An Echo In Here?"
};
public String ProcessInput(String TheInput)
{
String TheOutput = null;
if(State == WAITING)
{
TheOutput = "Knock! Knock!";
State = SENTKNOCKKNOCK;
}
else
if(State == SENTKNOCKKNOCK)
{
if(TheInput.equalsIgnoreCase("who's there?"))
{
TheOutput = Clues[CurrentJoke];
State = SENTCLUE;
}
else
{
TheOutput = "You're Supposed To Say \"Who's There?\"! "
+"Try Again. Knock! Knock!";
}
}
else
if(State == SENTCLUE)
{
if(TheInput.equalsIgnoreCase(Clues[CurrentJoke]+" Who?"))
{
TheOutput = Answers[CurrentJoke]+" Want Another? (y/n)";
State = ANOTHER;
}
else
{
TheOutput = "You're Supposed To Say \""
+Clues[CurrentJoke]+" Who?\"!"
+"Try Again. Knock! Knock!";
State = SENTKNOCKKNOCK;
}
}
else
if(State == ANOTHER)
{
if(TheInput.equalsIgnoreCase("y"))
{
TheOutput = "Knock! Knock!";
if(CurrentJoke == (NUMJOKES-1))
{
CurrentJoke = 0;
}
else { CurrentJoke++; }
State = SENTKNOCKKNOCK;
}
else
{
TheOutput = "Bye.";
State = WAITING;
}
}
return TheOutput;
}
}
Thank You In Advance, Please SomeOne Help.
Re: Problem connection client to server (Unknown Source)
Could you show the consoles from when you execute the code to show the args passed to main().
For easier testing, hardcode the values in the program so it easier to start and execute the code without having to pass it args on the commandline.
2 Attachment(s)
Re: Problem connection client to server (Unknown Source)
Thank you Sir Norm for your interest to help and sorry for taking long to reply it's so busy with work at this time of year.
Quote:
Could you show the consoles from when you execute the code to show the args passed to main().
Here are the screenshot:
Attachment 4033
Attachment 4034
Quote:
For easier testing, hardcode the values in the program so it easier to start and execute the code without having to pass it args on the commandline.
The IP isn't static so i thought it would be easier to pass it thought args on the commandline instead of having to recompile the code every time the code would be tested as the IP address may change.
Thank You In Advance :D
Re: Problem connection client to server (Unknown Source)
I have not setup my system to work using an external IP address. I can only test with localhost.
If the program works using localhost, then you need to check for problems with things like firewalls, routers etc.
To copy the contents of the command prompt window:
Click on Icon in upper left corner
Select Edit
Select 'Select All' - The selection will show
Click in upper left again
Select Edit and click 'Copy'
Paste here.
Re: Problem connection client to server (Unknown Source)
Quote:
To copy the contents of the command prompt window:
Click on Icon in upper left corner
Select Edit
Select 'Select All' - The selection will show
Click in upper left again
Select Edit and click 'Copy'
Paste here.
Code:
C:\Documents and Settings\Administrator\Bureau\KKSC>java KnockKnockServer 4444
Exception in thread "main" java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at sun.nio.cs.StreamDecoder.readBytes(Unknown Source)
at sun.nio.cs.StreamDecoder.implRead(Unknown Source)
at sun.nio.cs.StreamDecoder.read(Unknown Source)
at java.io.InputStreamReader.read(Unknown Source)
at java.io.BufferedReader.fill(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at KnockKnockServer.main(knockknockserver.java:50)
C:\Documents and Settings\Administrator\Bureau\KKSC>
Thank You In Advance ;D
Re: Problem connection client to server (Unknown Source)
When I execute the programs I get this:
Quote:
Running: java KnockKnockProgram
S got connection soc=Socket[addr=/127.0.0.1,port=59241,localport=8880]
C - from Server: Knock! Knock!
C - Sending to Server: who's there?
S read IL=who's there?
C - from Server: Turpin
C - Sending to Server: Turpin Who?
S read IL=Turpin Who?
C - from Server: Turpin The Heat, It's Cold In Here! Want Another? (y/n)
C - Sending to Server: n
S read IL=n
S - Bye Bye.
C - from Server: Bye.
exiting main
0 error(s)
My testing code: Code:
final static String ThePort = "8880";
public static void main(final String[] args) { //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Thread t1 = new Thread(new Runnable() {
public void run(){
KnockKnockServer.main(new String[]{ThePort});
}
});
t1.start();
Thread t2 = new Thread(new Runnable() {
public void run() {
KnockKnockClient.main(new String[]{"127.0.0.1", ThePort});
}
});
t2.start();
// wait some
try{Thread.sleep(5000);}catch(Exception x){}
System.out.println("exiting main");
System.exit(0); // done
}
Re: Problem connection client to server (Unknown Source)
when i test the program on my local host it works fine, but when attempting to make the altered code to connect two computers on the world wide web it doesn't work, maybe my altered code misses something. That's my problem.
Thank You In Advance.
Re: Problem connection client to server (Unknown Source)
What about routers and firewalls, etc Something outside of your program.
Re: Problem connection client to server (Unknown Source)
I do not think you have your computer, your routers, and your firewalls are set up properly to work on the world wide web. Also, making them open can be a security hole but putting them on a higher port number will help you prevent that. The port number you have chosen is way too low. You should choose one above 49151. The ones above this number are not registered. So 50000 would work.
Re: Problem connection client to server (Unknown Source)
Thank you Sir Norm and Mr kammce for your reply, I'll disable my firewall and test my code.
I had made a checkers game presented on JApplet, now i want to step further and learn how to make the game playable between two players over the WWW, that's why i searched for client server communication, the only useful thing i found was Writing the Server Side of a Socket (The Java™ Tutorials > Custom Networking > All About Sockets) tutorial, but afterward seems to me it's not enough.
Would You Guide Me Please To The Right Path, Thank You In Advance ;D
Re: Problem connection client to server (Unknown Source)
Please describe in detail how you want the clients and server to communicate? Are the clients executing the applets?
There are many sample client server programs here on the forum that you should be able to get some ideas from.
Re: Problem connection client to server (Unknown Source)
First step, i would like to learn to make one computer executes the game's applet as a server and the other computer would executes the game's applet as a client, establish a socket communication, transfer the data between them, and each computer-end process and implements the received data.
Second step, i would like to learn to make the game executed on a host server then get the two players (they are both clients in this case) establish a connection to the server and transfer the data between the server and the two clients to make the game (applet) playable.
Thank You In Advance, I Really Appreciate Your Help.
Re: Problem connection client to server (Unknown Source)
Quote:
one computer executes the game's applet as a server and the other computer would executes the game's applet as a client,
That does not make sense. An applet executes on a client's machine, not on a server.
Re: Problem connection client to server (Unknown Source)
Quote:
Originally Posted by
Norm
That does not make sense. An applet executes on a client's machine, not on a server.
Please don't mind my luck of knowledge on java networking.
From this tutorial: Writing the Server Side of a Socket (The Java™ Tutorials > Custom Networking > All About Sockets), you can see on the server-side, the user can type chain of characters and send them to the client, so i thought an applet can be executed on a server machine.
Can you advice me what readings should i start with? Thank You.
Re: Problem connection client to server (Unknown Source)
Sorry, I don't have any links for reading material for networking.
Re: Problem connection client to server (Unknown Source)
After some readings i found that you use servlets for server-side programming instead of applets.
Quote:
Originally Posted by
Norm
Sorry, I don't have any links for reading material for networking.
I am not asking for links to physical materials, what i am asking for is the topics i should look for and start with, and maybe a name for a book or two, Thank You.
Re: Problem connection client to server (Unknown Source)
Look at Sockets and ServerSockets.
Go through the tutorial:
The Really Big Index