Hey
I'm doing a school assigment and I met a problem which I need some help to figure out whats happening. I must say I havn't touched java and networking before. I did use this guide: http://java.sun.com/docs/books/tutorial/networking/sockets/clientServer.html, to write the code I have so far. I can't seem to figure out what is wrong. I am able to send a message from the server to the client, but then both client and server is just hanging (prob each in their own while loop). There's probably a obiouse logical error here, but I'm not able to spot it.
The assignment is to make a tic tac toe game with network support. I havn't impemented the network bit to the game, just doing some simple testing first, but the idea here is that the client waits on the server app to make a move, then it's the clients turn while the server app waits on the client and so on. Client will call the sendToServer() method (with parameters of which bricks has been added but havn't added them yet), and server will cal the sendToClient(). Probably would be best to have it in a new thread, but I want to keep it simple and the teacher havn't spesified that we need to have it in a new thread. Whats in the code now is just dummy values to test.
Cheers, and any help would be greatly apriciated :)
Alright here's the code:
package data;
import java.net.*;
import java.io.*;
import controller.Controller;
public class TicTacToeServer {
private ServerSocket serverSocket = null;
private Socket clientSocket = null;
private PrintWriter out = null;
private BufferedReader in = null;
TicTacToeProtocol tttProtocol = null;
public void hostServer() {
try {
serverSocket = new ServerSocket(7777);
}
catch (IOException e) {
System.err.println("Server Could not listen on port: " + serverSocket.getLocalPort());
System.exit(1);
}
try {
clientSocket = serverSocket.accept();
out = new PrintWriter(clientSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
}
catch (IOException e) {
System.err.println("Server Accept failed.");
System.exit(1);
}
tttProtocol = new TicTacToeProtocol();
Controller.setMultiplayer(true);
sendToClient();
}
public void sendToClient() {
String outputLine;
outputLine = serverTurn();
out.println(outputLine);
if (outputLine.equals("Bye."))
closeConnection();
else{
reciveFromClient();
}
}
public void reciveFromClient() {
String inputLine;
try{
while ((inputLine = in.readLine()) != null) {
fromClient(inputLine);
System.out.println("Server: " + inputLine);
tttProtocol.processInput(inputLine);
//outputLine = kkp.processInput(inputLine);
//System.out.println(inputLine);
}
}
catch (Exception e) {
System.out.println("Server Error Recive From client: " + e);
}
sendToClient();
}
public void closeConnection() {
try{
out.close();
in.close();
clientSocket.close();
serverSocket.close();
Controller.setMultiplayer(false);
}
catch (Exception e) {
System.out.println("Server Error Close Connection: " + e);
}
}
private String serverTurn(){
return "Yeah from Server!";
}
private void fromClient(String bricksMoved){
}
}
package data;
import gui.MainFrame;
import java.io.*;
import java.net.*;
import controller.Controller;
public class TicTacToeClient{
private Socket kkSocket = null;
private PrintWriter out = null;
private BufferedReader in = null;
public void connectToServer(InetAddress ipAddress) {
try {
kkSocket = new Socket(ipAddress, 7777);
out = new PrintWriter(kkSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(kkSocket.getInputStream()));
}
catch (UnknownHostException e) {
System.err.println("Client Don't know about host: " + ipAddress.getHostAddress());
System.exit(1);
}
catch (IOException e) {
System.err.println("Client Couldn't get I/O for the connection to: " + ipAddress.getHostAddress());
System.exit(1);
}
Controller.setMultiplayer(true);
reciveFromServer();
}
public void sendToServer() {
String fromUser;
//stdIn = new BufferedReader(new InputStreamReader(System.in));
fromUser = clientTurn();
if (fromUser != null) {
out.println(fromUser);
}
//reciveFromServer();
}
public void reciveFromServer() {
String fromServer;
try{
while ((fromServer = in.readLine()) != null) {
System.out.println("Server: " + fromServer);
serverTurn(fromServer);
if (fromServer.equals("Bye.")){
closeConnection();
break;
}
}
}
catch (Exception e) {
System.out.println("Client Error Recive From server: " + e);
}
sendToServer();
}
public void closeConnection(){
try{
out.close();
in.close();
kkSocket.close();
}
catch (Exception e) {
System.out.println("Client Error Close connection: " + e);
}
Controller.setMultiplayer(false);
}
private void serverTurn(String bricksMoved){
}
private String clientTurn(){
return "Yeah from client!";
}
}