Java Network

I am making a simple network using Socket .

It works great, but the problem is that it works like a board game.

Each time the server has to wait for the client, the client will wait for the server, etc.

I want data to be sent from server to client and from client to server whenever I enter data from either side.

Here is part of my server code

 in = Integer.parseInt(myInputStream.readLine())); // server gets data out = new Scanner(System.in).nextInt(); myOutputStream.println(column); // server sends data 
0
source share
3 answers

In one word: streams. Each application requires multiple threads. In particular, there should be threads designed to maintain queues of incoming and outgoing messages at each end, so the code that wants to send or receive a message does not have to wait.

This is a very big topic - I canโ€™t show you what to do. I would recommend the Concurrency chapter of the Java tutorial to get started.

+2
source

I want the data to be sent from the server to the client and from client to server whenever I enter data from either side.

To achieve the above, you need your client and server to be multi-threaded . Thus, although one thread on each side listens for data coming from the other partner, message processing is actually performed by the other thread.

If you havenโ€™t done multi-threaded programming in any language before this can be difficult, follow these steps:

Multithreaded Client / Server Applications

You can then go to the following link to improve your game structuring ideas:

Network Programming Example: Network Gaming Infrastructure

0
source
 Server.java import java.io.*; import java.net.*; public class Server implements Runnable { String messageIN, messageOUT; Thread t1 = null, t2 = null; BufferedReader b1, b2; ServerSocket ss; Socket s; PrintWriter p1; Server() { try { ss = new ServerSocket(8000); System.out.println(); System.out.println("Server is Waiting . . . . . "); s = ss.accept(); System.out.println(); System.out.println("Client Connected ! ! ! "); t1 = new Thread(this); t2 = new Thread(this); t1.start(); t2.start(); } catch (Exception e) { System.out.println(e); } } //---------------------------------------------------------------------------------------- public void run() { if (Thread.currentThread() == t1) { try { b1 = new BufferedReader(new InputStreamReader(System.in)); p1 = new PrintWriter(s.getOutputStream(), true); do { System.out.println(); messageIN = b1.readLine(); System.out.println("Server Says : : : "+messageIN); p1.println(messageIN); } while (!messageIN.equals("END")); } catch (Exception ex) { } } else { try { b2 = new BufferedReader(new InputStreamReader(s.getInputStream())); do { messageOUT = b2.readLine(); System.out.println("Client Says : : : " + messageOUT); System.out.println(); } while (!messageOUT.equals("END")); } catch (Exception e) { } } } //---------------------------------------------------------------------------------------- public static void main(String[] args) { new Server(); } } //----------------------------------------------------------------------------------------- 

Client.java

 import java.io.*; import java.net.*; public class Client implements Runnable { String messageIN, messageOUT; Thread thread1 = null, thread2 = null; BufferedReader br1, br2; Socket s; PrintWriter pw; Client() { try { System.out.println(); System.out.println("Going to connect to Server"); s = new Socket("localhost", 8000); System.out.println(); System.out.println("Connected"); thread1 = new Thread(this); thread2 = new Thread(this); thread1.start(); thread2.start(); } catch (Exception ex) { System.out.println("ex = " + ex); } } //----------------------------------------------------------------------------------------- public void run() { if (Thread.currentThread() == thread2) { try { br1 = new BufferedReader(new InputStreamReader(System.in)); do { System.out.println(); messageIN = br1.readLine(); System.out.println("Client Says : : "+messageIN); pw = new PrintWriter(s.getOutputStream(), true); pw.println(messageIN); } while (!messageIN.equals("END")); } catch (Exception ex) { } } else { try { do { br2 = new BufferedReader(new InputStreamReader(s.getInputStream())); messageOUT = br2.readLine(); System.out.println("Server Says : : : " + messageOUT); System.out.println(); } while (!messageOUT.equals("END")); } catch (Exception e) { } } } //---------------------------------------------------------------------------------------- public static void main(String[] args) { new Client(); } } 
0
source

All Articles