[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Java - quick review of RTS client TCP server setup code please

Posted on 2014-03-11
11
Medium Priority
?
397 Views
Last Modified: 2014-03-25
Hi
I've been asking a few Python RTS Q's, but I've decided to go back to java, based on recommendations in another question. - I'm thinking a lot clearer now, from all the mistakes  understood.

i'm going to start his time with a TCP connecting system for complete reliability, and then an in-game UDP cycle with TCP aid

First I'm doing the initial connection with TCP. Please check it out, the client socket is missing something initially, - Should not be difficult to fix, below server client

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

public class DRWserver extends Thread {
	
	public int numClients, numClientsExpected;
	
	serverClient[] serverClients;
	public static int TCPport=1024;
	
	
	ServerSocket serverSocket;

	public DRWserver( int playerCount) {
		
		numClientsExpected=playerCount;
		
		serverClients = new serverClient[numClientsExpected];
		numClients=0;
		
		start();
		
	}
	
	public void run()  {
		
		DataOutputStream dos;
		DataInputStream is;
		try {
			
		serverSocket = new ServerSocket(TCPport);
		Thread.sleep(200);
		System.out.println("Created TCP serverSocket on port "+serverSocket.getLocalPort());
		boolean AcceptingClients=true;
		
		
		
		
		while (AcceptingClients) {


			System.out.println("\nClient TCP accept() : ");
			Socket socket = serverSocket.accept();
			

			System.out.println("Client joining"); // needs serverClients to be added to
			System.exit(0);
		}
		
		
		
		
		} catch (Exception e) {

			System.out.println("\nDRWserver run ex : "+e);
			e.printStackTrace();
		}
		}
	
	public static void main(String[] args) {

		
		new DRWserver(3);
		
		

	}

}

Open in new window



server needs server client stuct . . .
server client . .
import java.net.*;


public class serverClient {
	
	int playerNumber;
	Inet4Address address;

}

Open in new window





client code . . . .
import java.io.*;
import java.net.*;


public class DRWclient extends Thread{
	
	InetAddress serverAddress;
	int clientNumber;
	Socket clientSocket;
	
	public static byte MSG_JOIN=1;

	public DRWclient(int cliNum, InetAddress serveraddress) {
		
		clientNumber=cliNum;
		serverAddress= serveraddress;
		
		start();
	}
	
	public void run() {
		try {
		System.out.println("Client "+clientNumber+" run()");
		
		clientSocket= new Socket();
		while (!clientSocket.isConnected()) {
			clientSocket= new Socket();
			System.out.print("c");
		}
		System.out.println("Socket connected");
		DataOutputStream os = new DataOutputStream(clientSocket.getOutputStream());
		
		byte[] bytes = new byte[64];
		bytes[0]=MSG_JOIN;
		byte[] BAplayerNum = intToByteArray(clientNumber);
		bytes[1]=BAplayerNum[0];
		bytes[2]=BAplayerNum[1];
		bytes[3]=BAplayerNum[2];
		bytes[4]=BAplayerNum[3];
		DatagramPacket packet = new DatagramPacket(bytes,64);
		
		packet.setAddress(serverAddress);
		
		os.write(bytes); // should be packet
		
		
		} catch (Exception e) {
			
			System.out.println("DRWclient run() ex : "+e);
			e.printStackTrace();
			
		}
		
		
		
		
	}
	public static void main(String[] args) {

		InetAddress servAddress=null;
		try {
			servAddress = InetAddress.getByName("x.y.c.h server ip address");
		} catch (Exception e) {
			System.out.println("DRWclient main() ex : "+e);
			e.printStackTrace();
		}
		DRWclient c0 = new DRWclient(0,servAddress);

	}
	
	public static int byteArrayToInt(byte[] b) {
		return byteArrayToInt(b, 0);
		}
	
	
	public static int byteArrayToInt(byte[] b, int offset) {
		int value = 0;
		for (int i = 0; i < 4; i++) {
		int shift = (4 - 1 - i) * 8;
		value += (b[i + offset] & 0x000000FF) << shift;
		}
		return value;
		}
	
	public static byte[] intToByteArray(int value) {
		byte[] b = new byte[4];
		for (int i = 0; i < 4; i++) {
		int offset = (b.length - 1 - i) * 8;
		b[i] = (byte) ((value >>> offset) & 0xFF);
		}
		return b;
		}
	

}

Open in new window

0
Comment
Question by:beavoid
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 2
  • 2
11 Comments
 
LVL 36

Expert Comment

by:mccarl
ID: 39922564
You need to create the client Socket with the address of the server, otherwise it just stays as an unconnected socket...
clientSocket = new Socket(serverAddress, 1024);

Open in new window

0
 
LVL 86

Accepted Solution

by:
CEHJ earned 2000 total points
ID: 39922810
Your design looks back to front.

public class DRWserver extends Thread {
	
	public int numClients, numClientsExpected;
	
	serverClient[] serverClients;

Open in new window


The server should use n threads to handle n connections. It should not be one thread accepting n connections. Now, that might be OK if the server itself ran in its own thread (though you wouldn't extend Thread) and then spawned further threads to handle the actual connections, but i can see you're not doing that.

Personally i would use a framework for basic CS functionality and then build your app around that.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39925701
?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:beavoid
ID: 39926184
Thanks. - if I have a pool of threads handling activity messages from each client, where should I house the game state best, and the clock? Must the clock wait for an activity message from each client before incrementing? - like a game of monopoly needs a move from everyone?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39926673
Sorry - i'm get confused. You asked for a review of your code, which i gave you and you ignored - now you're asking another question after closing it ..??
0
 

Author Comment

by:beavoid
ID: 39926742
CEHJ, I apologize for not giving you credit for your excellent answer. I am asking moderators to let me re-assign credit. I just quickly pressed accept answer on the answer with the socket code I was hoping for. - and not multiple solutions.

mods, can you re-open the closing for me?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39926812
Oh OK. You need to click on this to get attention
Having said that, you still need to open a new question
0
 
LVL 36

Expert Comment

by:mccarl
ID: 39952416
I just quickly pressed accept answer on the answer with the socket code I was hoping for. - and not multiple solutions
And now you have gone and still not selected multiple solutions? And my answer which was directly addressing your initial question goes unrewarded?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39952769
:)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39952771
Oh dear! Didn't notice there was a problem still ...
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Introduction to Processes
Suggested Courses

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question