Making java onlinemaking an online game in java
25 Replies - 2667 Views - Last Post: 20 June 2008 - 09:59 AM
#1
Making java online
Posted 19 June 2008 - 06:56 PM
Replies To: Making java online
#2
Re: Making java online
Posted 19 June 2008 - 07:26 PM
#3
Re: Making java online
Posted 19 June 2008 - 07:29 PM
#4
Re: Making java online
Posted 19 June 2008 - 07:32 PM
#5
Re: Making java online
Posted 19 June 2008 - 07:59 PM
kkSocket = new Socket("name of machine on network", 4444);
i tryed to put my Ip adress but the client still couldn't connect.
#6
Re: Making java online
Posted 19 June 2008 - 08:11 PM
thabirdy, on 19 Jun, 2008 - 07:59 PM, said:
No, believe it or not your are posting here using HTTP over a socket.
Check my snippet (cut & paste the code and try it on your system).
http://www.dreaminco...snippet1917.htm
Should work. "localhost" is the name of the local machine but the IP address like 192.168.1.100 will also work.
#7
Re: Making java online
Posted 19 June 2008 - 08:16 PM
Here is my code...might help...BTW its not my code....its from Java lession
import java.net.*;
import java.io.*;
public class KnockKnockServer {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(4444);
} catch (IOException e) {
System.err.println("Could not listen on port: 4444.");
System.exit(1);
}
Socket clientSocket = null;
try {
clientSocket = serverSocket.accept();
} catch (IOException e) {
System.err.println("Accept failed.");
System.exit(1);
}
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(
new InputStreamReader(
clientSocket.getInputStream()));
String inputLine, outputLine;
KnockKnockProtocol kkp = new KnockKnockProtocol();
outputLine = kkp.processInput(null);
out.println(outputLine);
while ((inputLine = in.readLine()) != null) {
outputLine = kkp.processInput(inputLine);
out.println(outputLine);
if (outputLine.equals("Bye."))
break;
}
out.close();
in.close();
clientSocket.close();
serverSocket.close();
}
}
heres the client
import java.io.*;
import java.net.*;
public class KnockKnockClient {
public static void main(String[] args) throws IOException {
Socket kkSocket = null;
PrintWriter out = null;
BufferedReader in = null;
try {
kkSocket = new Socket("taranis", 4444);
out = new PrintWriter(kkSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(kkSocket.getInputStream()));
} catch (UnknownHostException e) {
System.err.println("Don't know about host: taranis.");
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for the connection to: taranis.");
System.exit(1);
}
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
String fromServer;
String fromUser;
while ((fromServer = in.readLine()) != null) {
System.out.println("Server: " + fromServer);
if (fromServer.equals("Bye."))
break;
fromUser = stdIn.readLine();
if (fromUser != null) {
System.out.println("Client: " + fromUser);
out.println(fromUser);
}
}
out.close();
in.close();
stdIn.close();
kkSocket.close();
}
}
and heres a other class i use
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 = { "Turnip", "Little Old Lady", "Atch", "Who", "Who" };
private String[] answers = { "Turnip 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;
}
}
I think it most be an forwarding port issue
This post has been edited by thabirdy: 19 June 2008 - 08:20 PM
#8
Re: Making java online
Posted 19 June 2008 - 08:25 PM
kkSocket = new Socket("taranis", 4444);
And you have a DNS server that says that "tanaris" is your server ?
Try
kkSocket = new Socket("localhost", 4444);
just for fun
This post has been edited by pbl: 19 June 2008 - 08:27 PM
#9
Re: Making java online
Posted 19 June 2008 - 08:32 PM
I even tryed using code from your link. But i change the server name again and the port on your code...so that line 18 (your code) is now socket = new Socket("192.168.2.10",1500);
This post has been edited by thabirdy: 19 June 2008 - 08:34 PM
#10
Re: Making java online
Posted 19 June 2008 - 08:40 PM
thabirdy, on 19 Jun, 2008 - 08:32 PM, said:
I even tryed using code from your link. But i change the server name again and the port on your code...so that line 18 (your code) is now socket = new Socket("192.168.2.10",1500);
Cut & pasted your code
Test it with: localhost and 192.168.1.100 which is my address
worked well with both
P.S.
Thabirdy ignore the following line it is an inline joke not for you
Not sure that KnockKnockProtocol is as good as FTP 1.0 from Kualam Lupur but it works.
#11
Re: Making java online
Posted 19 June 2008 - 08:46 PM
//The client code Client.java:
import java.net.*;
import java.io.*;
public class Client {
ObjectInputStream Sinput; // to read the socker
ObjectOutputStream Soutput; // towrite on the socket
Socket socket;
// Constructor connection receiving a socket number
Client(int port) {
// we use "localhost" as host name, the server is on the same machine
// but you can put the "real" server name or IP address
try {
socket = new Socket("192.168.1.100", 1500);
}
catch(Exception e) {
System.out.println("Error connectiong to server:" + e);
return;
}
System.out.println("Connection accepted " +
socket.getInetAddress() + ":" +
socket.getPort());
/* Creating both Data Stream */
try
{
Sinput = new ObjectInputStream(socket.getInputStream());
Soutput = new ObjectOutputStream(socket.getOutputStream());
}
catch (IOException e) {
System.out.println("Exception creating new Input/output Streams: " + e);
return;
}
// now that I have my connection
String test = "aBcDeFgHiJkLmNoPqRsTuVwXyZ";
// send the string to the server
System.out.println("Client sending \"" + test + "\" to serveur");
try {
Soutput.writeObject(test);
Soutput.flush();
}
catch(IOException e) {
System.out.println("Error writting to the socket: " + e);
return;
}
// read back the answer from the server
String response;
try {
response = (String) Sinput.readObject();
System.out.println("Read back from server: " + response);
}
catch(Exception e) {
System.out.println("Problem reading back from server: " + e);
}
try{
Sinput.close();
Soutput.close();
}
catch(Exception e) {}
}
public static void main(String[] arg) {
new Client(1500);
}
}
thats only when connecting to your computer...when try to connect to mine i have a friend who is running the client and its code is
//The client code Client.java:
import java.net.*;
import java.io.*;
public class Client {
ObjectInputStream Sinput; // to read the socker
ObjectOutputStream Soutput; // towrite on the socket
Socket socket;
// Constructor connection receiving a socket number
Client(int port) {
// we use "localhost" as host name, the server is on the same machine
// but you can put the "real" server name or IP address
try {
socket = new Socket("192.168.2.10", 1500);
}
catch(Exception e) {
System.out.println("Error connectiong to server:" + e);
return;
}
System.out.println("Connection accepted " +
socket.getInetAddress() + ":" +
socket.getPort());
/* Creating both Data Stream */
try
{
Sinput = new ObjectInputStream(socket.getInputStream());
Soutput = new ObjectOutputStream(socket.getOutputStream());
}
catch (IOException e) {
System.out.println("Exception creating new Input/output Streams: " + e);
return;
}
// now that I have my connection
String test = "aBcDeFgHiJkLmNoPqRsTuVwXyZ";
// send the string to the server
System.out.println("Client sending \"" + test + "\" to serveur");
try {
Soutput.writeObject(test);
Soutput.flush();
}
catch(IOException e) {
System.out.println("Error writting to the socket: " + e);
return;
}
// read back the answer from the server
String response;
try {
response = (String) Sinput.readObject();
System.out.println("Read back from server: " + response);
}
catch(Exception e) {
System.out.println("Problem reading back from server: " + e);
}
try{
Sinput.close();
Soutput.close();
}
catch(Exception e) {}
}
public static void main(String[] arg) {
new Client(1500);
}
}
he gets a timed out error too
#12
Re: Making java online
Posted 19 June 2008 - 08:56 PM
thabirdy, on 19 Jun, 2008 - 08:46 PM, said:
Oups... a lot of stuff
OK you cannot connect to my server ... first it is not running second there are million of 192.168.x.x addresses in the world
Addresses 192.168.x.x are for local area ... there are not enough IP addresses for all computer in the world... so your provider got an address like 1.2.3.4 and all its clients have adresses in 192.168.x.x
When you make a request on the Internet the request is send form 192.168.1.44 of 1.2.3.4 and the answer is forwarded there
So go back to YOUR 192.168.2.10 (I think)
Now you have another problem... your Server is listening to port 4444
and in your client you pass the port number as parameter to your client constructor
public class Client {
ObjectInputStream Sinput; // to read the socker
ObjectOutputStream Soutput; // towrite on the socket
Socket socket;
// Constructor connection receiving a socket number
Client(int port) {
// we use "localhost" as host name, the server is on the same machine
// but you can put the "real" server name or IP address
}
public static void main(String[] arg) {
new Client(1500);
}
}
As far as I see in your main() method you try to connect on port 1500 which is not 4444
#13
Re: Making java online
Posted 19 June 2008 - 09:01 PM
#14
Re: Making java online
Posted 19 June 2008 - 09:08 PM
#15
Re: Making java online
Posted 19 June 2008 - 09:16 PM
|
|

New Topic/Question
Reply




MultiQuote




|