Hi!

I want to put up a network with a server and a client. The game shall be implemented in the server. The tictactoe game itself works fine.And I've just started to implement it as you can see from the code. There is supposed to be two players i e two clients and a server from were the game is run.
(The game is supposed to start asking "Do you want the game to act as a server or as a client? s = server, c = client ". But don't bother about this now.)
The problem occurs when I've typed in position and player in the commandprompt. Then the game jumps out of the process and ends. But my intention is that the player shall be able to see gameboard. But this doesn't happen.
With the statement t.setVal(pos, player); I'm setting the position and player and then I'm sending the whole game back to the client;
outToClient.writeBytes(t.toString() + '\n');


Here I want the players at the client side to see the gameboard.

The toString method works fine when just playing the game without networking.

feel kind of stuck here

Can anyone help me here?

below follows the code

import java.net.*;
import java.io.*;
import java.util.Scanner;
import java.lang.String;

class ServerClient
{

public static void main(String argv[]) throws Exception
{
	Scanner scan = new Scanner (System.in);
	
	System.out.println("Do you want the game to act as a server or as a client? s = server, c = client ");
	String answer = scan.nextLine(); 
	LuffarSchack t = new LuffarSchack();
	
	if (answer.equals("s"))
	{
	String  clientSentence;
	String capitalizedSentence;
	
	ServerSocket welcomeSocket = new ServerSocket(6789);
	
	while (true)
	{
		try
        {
        	InetAddress address = InetAddress.getLocalHost();
        	System.out.println(address);
        	
        }
        catch (UnknownHostException e)
        {
        	System.out.println("Could not find local address!");
        }
		
			
		Socket connectionSocket = welcomeSocket.accept();
		
		BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
		
		DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
		
		 
		
		/*clientSentence = inFromClient.readLine();
		
		capitalizedSentence = clientSentence.toUpperCase();
		
		outToClient.writeBytes(capitalizedSentence + '\n');*/
		
		
		
		
		 
		String position = inFromClient.readLine(); 
			
		int pos = Integer.parseInt(position);	
		
		String playerS = inFromClient.readLine();
		 
		int player = Integer.parseInt(position);
		
		t.setVal(pos, player);
   
   		/*String visaSpel = t.toString();*/
   		
   		
   		outToClient.writeBytes(t.toString() + '\n');
	
		/*w = t.winner();*/
		
			
		
	}
	}
	 if (answer.equals("k"))
	{
		
		String sentence;
        String modifiedSentence;
        
        BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
        
        Socket clientSocket = new Socket(InetAddress.getLocalHost(), 6789);
		
		DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
		
		BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
		
		
		
		sentence = inFromUser.readLine();
		
		outToServer.writeBytes(sentence + '\n'); 
					
		modifiedSentence = inFromServer.readLine();
		
		System.out.println("FROM SERVER: " + modifiedSentence );
		
		clientSocket.close();
	} 
	}	
}

and the client

import java.net.*;
import java.io.*;


public class TCPClient {
        
    public static void main(String argv[] ) throws Exception{
        // TODO code application logic here
        
        String sentence;
        String modifiedSentence;
        
        BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
        
        Socket clientSocket = new Socket(InetAddress.getLocalHost(), 6789);
		
		DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
		
		BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
		
		
		/*	sentence = inFromUser.readLine();
		
		outToServer.writeBytes(sentence + '\n'); 
					
		modifiedSentence = inFromServer.readLine();
		
		System.out.println("FROM SERVER: " + modifiedSentence );*/
		
		
		
		System.out.println("Ange spelare X (dvs ange 1 )  eller spelare O (dvs ange 2): " );
		
		String player = inFromUser.readLine();
		
		outToServer.writeBytes(player + '\n');
		
		System.out.println("Give position : " );
		
		String position = inFromUser.readLine();
		
		outToServer.writeBytes(position + '\n');
		
		String showgame =  inFromServer.readLine();
		
		System.out.println(showgame );
		clientSocket.close();
        
        
    }
}

The game ends because you closed the socket between the client/server pair and because you have no while loop or other loop structure to facilitate each player taking turns. And I don't see why you're asking whether each should be a client or a server. You can't run a client without the server first being active anyway, so you should run the server first, then run the client. And the Client should have a separate class than the Server.

You should be using Sockets in order to communicate between your client(s) and your server. Check out this tutorial, it explains in detail and it has example code for single client-server pairs and for a server that can handle multiple clients.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.