Java networking
I'm making a simple network using Socket
.
It works fine but the problem is it's working like a board game
Every time the server has to wait for the开发者_如何学C client then the client will wait for the server and so on.
I want the data to be sent from server to client and from client to server whenever I enter data from any side.
Here is a part of my code in server
in = Integer.parseInt(myInputStream.readLine())); // server gets data
out = new Scanner(System.in).nextInt();
myOutputStream.println(column); // server sends data
In a single word: threads. Your application needs multiple threads on each end. In particular, there should be threads devoted to maintaining queues of incoming and outgoing messages on each end, so that code wanting to send or receive a message does not have to wait.
This is a very big topic -- I can't really show you exactly what to do. I'd recommend the Concurrency chapter of the Java Tutorial to get started.
i want the data to be sent from server to client and from client to server whenever i enter data from any side..
In order to achieve the above, you need both your client and server to be multi-threaded
. So, while one thread on each side listens to data coming in from the other peer, the processing of the message is actually done by another thread.
If you haven't done multi-threaded programming in any language before this might be a daunting task, so following is a link to start with:
Multithreaded Client/Server Applications
Then you can go to the following link for better ideas of structuring your game:
Network Programming Example: A Networked Game Framework
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();
}
}
精彩评论