I've built simple client-server model using sockets. The server receives 1 type of request: 2 numbers from client, sums them, waits for 5 seconds and sends the response back to the client.
I'm trying to send 20 asynchronous request from the client without waiting for response.
The client should sums all the numbers from all the 20 Reponses from server. I'm trying to understand what should I use and how? Threads on the server, or the client and how?
I've added my client and server classes.
Server:
public class Server {
public static void main(String[] args) throws IOException {
try {
//Make a ServerSocket to listen for message
ServerSocket ss = new ServerSocket(7777);
while (true == true) {
//Accept input from socket
Socket s = ss.accept();
//Read input from socket
InputStreamReader streamReader = new InputStreamReader(s.getInputStream());
BufferedReader reader = new BufferedReader(streamReader);
String message = reader.readLine();
System.out.println(message);
//parse the json recieved from client and sum the 2 numbers
Object obj = new JSONParser().parse(String.valueOf(message));
JSONObject jo = (JSONObject) obj;
long num1 = (long) jo.get("num1");
long num2 = (long) jo.get("num2");
long sum = num1 + num2;
Thread.sleep(5000);
//putting response as json
JSONObject jsonResponse = new JSONObject();
jsonResponse.put("response", sum);
//get the message and write it to the socket as response
PrintWriter writer = new PrintWriter(s.getOutputStream());
writer.println(jsonResponse);
//System.out.println(df);
writer.close();
}
} catch (IOException | ParseException | InterruptedException ex) {
System.out.println(ex);
}
}
}
Client:
public class Client {
public static void main(String[] args) throws IOException {
try {
//this variable will sum all the responses from server
long sumOfAllResponses = 0;
for(int i = 0 ; i< 20; i++){
//Create a Socket with ip and port number
Socket s = new Socket("localhost", 7777);
Scanner in = new Scanner(System.in);
PrintWriter writer = new PrintWriter(s.getOutputStream());
InputStreamReader streamReader = new InputStreamReader(s.getInputStream());
BufferedReader reader = new BufferedReader(streamReader);
//Creating json with 2 numbers to be sent to server as a request
JSONObject jsonRequest = new JSONObject();
jsonRequest.put("num1", 1);
jsonRequest.put("num2", 1);
System.out.println(jsonRequest);
//Make a printWriter and write the message to the socket
writer.println(jsonRequest);
writer.flush();
//Get the response message from server
String responseMessage = reader.readLine();
//parse the response and add the result to the sum variable
Object obj = new JSONParser().parse(String.valueOf(responseMessage));
JSONObject jo = (JSONObject) obj;
sumOfAllResponses += (long) jo.get("response");
}
System.out.println(sumOfAllResponses);
}
catch (IOException | ParseException ex) {
ex.printStackTrace(); // (**)
}
}
}
Asynchronous means sending message and not waiting for response.
//Get the response message from server - so you wait whole 5 seconds for response :)
String responseMessage = reader.readLine();
The simplest solution in this case is to remove waiting for response each time. So, get rid of above lines from loop inside Client class.
In this particular client-sever case you do not need additional threads, if you would perform asynchronous things inside one application, then so. Take a look at Java Futures with some tutorial on how to use them.
But if you want to get a result from server, you have to wait anyway. And you want to get results of all calcuations. Hence, you have to store all incoming requests somewhere. Simple, naive and impractical, but showing asynchronicity concept code may look like this
public class Client {
public static void main(String[] args) throws IOException {
try {
long start = System.currentTimeMillis();
BufferedReader reader = null;
for(int i = 0 ; i < 20; i++){
Socket s = new Socket("localhost", 7777);
PrintWriter writer = new PrintWriter(s.getOutputStream());
InputStreamReader streamReader = new InputStreamReader(s.getInputStream());
reader = new BufferedReader(streamReader);
// just make sure you send data and do not wait for response
System.out.println("Sending " + i + " : " + (System.currentTimeMillis() - start));
writer.println(i);
writer.flush();
}
//this line works like future.get(), it hangs client until it receives result
String responseMessage = reader.readLine();
// process returned data as you want
System.out.println(responseMessage);
}
catch (IOException ex) {
ex.printStackTrace(); // (**)
}
}
}
public class Server {
public static void main(String[] args) throws IOException {
try {
//Make a ServerSocket to listen for message
ServerSocket ss = new ServerSocket(7777);
Socket s;
//we need to store incoming requests to process, and return them
List<String> integerList = new LinkedList<>();
while (true) {
s = ss.accept();
InputStreamReader streamReader = new InputStreamReader(s.getInputStream());
BufferedReader reader = new BufferedReader(streamReader);
String message = reader.readLine();
System.out.println(message);
// do something here
Thread.sleep(5000);
PrintWriter writer = new PrintWriter(s.getOutputStream());
integerList.add(message);
writer.println(integerList);
writer.close();
}
} catch (IOException | InterruptedException ex) {
System.out.println(ex);
}
}
}
Server side
The server should not handle requests on it's main thread.
Instead it should open handling thread for each request it gets.
We should limit the number of concurrent running threads.
Here is simple code example:
public class Server {
private static final int NUMBER_OF_CONCURRENT_THREADS = 20;
private ServerSocket serverSocket;
ExecutorService executor = Executors.newFixedThreadPool(NUMBER_OF_CONCURRENT_THREADS);
public void start(int port) throws IOException {
serverSocket = new ServerSocket(port);
while (true) {
final Socket clientSocket = serverSocket.accept();
final ClientHandler clientHandler = new ClientHandler(clientSocket);
executor.submit(clientHandler);
}
}
public void stop() throws IOException {
serverSocket.close();
}
private static class ClientHandler implements Runnable {
private Socket clientSocket;
public ClientHandler(Socket socket) {
this.clientSocket = socket;
}
public void run() {
// Implement your client logic here
}
}
}
Client side
Now you can send your request from the client side concurrently and they will be processed by the service concurrently (up to 20 requests at once)
I'll leave to you the implementation of the concurrent request sending, but here are some guidelines:
you can send each request in it's own thread, use ExecutorService to handle those threads
For each response from the service, add to shared variable holding the total sum of all responses.
Make sure to synchronize access to the shared total sum as multiple threads will be updating it in parallel.
Related
I'm trying to create a simple chat server that allows multiple distinct clients to chat with one another via the server output console. The clients each with their own thread write to the server and can view the results on the server's standard output via the console. However, I can't seem to get the BufferedReader to receive the messages coming from more than one client socket connection.
Currently the first client thread gains exclusive access to the BufferedReader via it's socket. However, I'd like multiple clients to connect to the server's input stream reader and have it wait for input from multiple client threads with distinct socket connections. I would like clients to be able to post to the server at the same time. How would I accomplish this with or without BufferedReader as my input stream reader?
public class chatServer {
public chatServer() throws IOException {
int PORT = 8189;
try (ServerSocket server = new ServerSocket(PORT)) {
System.out.println("The server is running at "
+ InetAddress.getByName(null) + "...");
String rules = "The rules of this server are as follows:\n"
+ "1.Respect your fellow chatters\n"
+ "2.Vulgar language will result in punishment\n"
+ "3.We reserve the right to ban you at any time.\n"
+ "Enjoy!";
System.out.println(rules + "\n");
while (true) {
try {
new clientHandler(server.accept()).run();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) throws IOException {
chatServer cs = new chatServer();
}
class clientHandler implements Runnable {
Socket socket;
public clientHandler(Socket socket) {
this.socket = socket;
}
public void run() {
try {
BufferedReader in = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
String line;
while (true) {
line = in.readLine();
if ((line == null) || line.equalsIgnoreCase("exit")) {
// socket.close();
} else {
System.out.println(socket.getPort() + " > " + line);
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
public class chatClient {
private Socket socket;
private String name = "";
private String IP = "127.0.0.1";
private int PORT = 8189;
public chatClient(String name, String IP, int PORT) throws UnknownHostException, IOException{
this.name = name;
socket = new Socket(this.IP,this.PORT);
}
public static void main(String[] args) throws UnknownHostException, IOException{
chatClient c1 = new chatClient("John",null,0);
chatClient.connect(c1);
}
public static void connect(chatClient cc) throws IOException {
Socket socket = cc.socket;
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
out.println("Welcome " + cc.name);
String message = "";
boolean done = false;
Scanner stdin = new Scanner(System.in);
System.out.println("Type your message here:");
while(!done){
System.out.print("> ");
message = stdin.nextLine();
out.println(message);
if(message.trim().equalsIgnoreCase("exit")){
done = true;
}
}
}
}
Update: I'm looking for a suitable/alternative method to achieve the functionality of a Server that accepts multiple posts from various clients with distinct socket connections? If my current implementation cannot do so then how may I modify it to do so?
A BufferedReader is constructed around a single reader, which in turn is directly or indirectly connected to a single data source. There is no way of changing that once constructed.
In short your question doesn't make sense. Each client has a distinct socket, which in turn has its unique input and out out streams, which in turn must be wrapped in a distinct BufferedTeader per client. Not one shared between all clients.
EDIT Your actual problem has nothing to do with BufferedReaders. It is here:
new clientHandler(server.accept()).run();
You're running the handler inline instead of as a separate thread. run() should be
new Thread(new clientHandler(socket.accept())).start();
I have this server and client application that counts how many times a button has been clicked. Some other members here were kind enough to solve my previous issue with it, and now I have another.
When I start up the server, and a client connects, and the client clicks the button, the counter on the server side goes up. However, when a second client connects to the server, the counter resets to 0. I am thinking that the server creates a seperate instance of the counter for some reason.
Here is the (updated) server sided code:
public class Server {
public static void main(String[] args) throws Exception {
Socket socket = null; //create a new socket
ServerSocket listener = new ServerSocket(9898);
System.out.println("The server is running!");
try {
while (true) {
new ClickServer(listener.accept()).start();
}
} finally {
listener.close();
}
}
/**
* A private thread to handle clicking requests
*/
private static class ClickServer extends Thread {
private Socket socket;
AtomicInteger totalBets = new AtomicInteger(0);
public void incrementTotalBets() {
totalBets.incrementAndGet();
}
public int getTotalBets() {
return totalBets.get();
}
public ClickServer(Socket socket) {
this.socket = socket;
log("New connection with client at " + socket);
}
public void run() {
try {
while (true) {
socket.setTcpNoDelay(true);
InputStream is = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String input = br.readLine();
OutputStream os = socket.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(osw);
if(input.equals("increment")) {
bw.write(String.valueOf(totalBets.incrementAndGet()));
bw.newLine();
bw.flush();
System.out.println("Total Bets: " + getTotalBets());
}
}
} catch (IOException e) {
log("Error handling client\n" + e);
} finally {
try {
socket.close();
} catch (IOException e) {
log("Error closing socket");
}
log("Connection with client closed");
}
}
private void log(String message) {
System.out.println(message);
}
}
}
The totalBets integer is the counter. When multiple client join the server, and click the button, it should increment the counter and send it back based on what the counter already is. It shouldn't reset for every new client.
Here is the relevant client-sided code:
public void actionPerformed(ActionEvent arg0) {
try {
OutputStream os = socket.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(osw);
String target = "";
bw.write("increment" + "\n");
bw.flush();
InputStream is = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String id = br.readLine();
System.out.println("bet added: " + id);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
I'm not sure why a new instance of the Server is being created every time a new client connects. I did some debugging, and the server knows there is more than one client connected to the server at the same port.
Any help?
Thank you.
NOTE: the ClickServer class is nested inside the Server class. It just doesn't look that way in the code box.
You'd need a shared counter that's thread safe. I'd go for
public static AtomicInteger totalBets = new AtomicInteger (0);
You'd have to change getter and setter some but this should share value between Clients. Or trash those methods and simply do
if(input.equals("increment")) {
bw.write(String.valueOf(totalBets.incrementAndGet ()));
Try changing :
public int totalBets = 0;
to:
public static int totalBets = 0;
Socket socket = new Socket("192.168.178.47", 82);
OutputStream out = socket.getOutputStream();
out.write("{ \"phone\": \"23456789\" }".getBytes());
out.flush();
//Server
InputStream in = client.getInputStream();
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
int i = 0;
while((i = in.read()) >= 0) {
bOut.write(i);
}
String complete = new String(bOut.toByteArray(), "UTF-8");
I had tried to send data via OutputStream to a socket but the data is not flushing. If I add an out.close(); to the end then it works perfectly, but the socket is closed and I cannot accept the response. Does anybody know why? The server is not giving any type of error. I had used Java 1.7!
It is possible that the server is waiting for the end of line. If this is the case add "\n" to the text
I'm not sure of the labelling "//Server" in your question, but I'm assuming the following code is the server code:
InputStream in = client.getInputStream();
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
int i = 0;
while((i = in.read()) >= 0) {
bOut.write(i);
}
String complete = new String(bOut.toByteArray(), "UTF-8");
This will continue to read, blocking each time, until it gets a value from read() less than zero. That only happens if the stream is closed.
It really looks like you need to establish your own protocol. So instead of looking for "<=0" look for some constant value that signals the end of the message.
Here's a quick demonstration of what I mean (I didn't have time yesterday). I have 3 classes, Message,MyClient (which also is the main class), and MyServer. Notice there isn't anything about sending or receiving a newline. Nothing is setting tcpNoDelay. But it works fine. Some other notes:
This code only sends and receives a single request and response.
It doesn't support sending multiple Message instances. That would require checking for the start of a Message as well as the end.
Message class:
public class Message {
public static final String MSG_START = "<message>";
public static final String MSG_END = "</message>";
private final String content;
public Message(String string){
content = string;
}
#Override
public String toString(){
return MSG_START + content + MSG_END;
}
}
MyServer class
public class MyServer implements Runnable{
public static final int PORT = 55555;
#Override
public void run(){
try {
ServerSocket serverSocket = new ServerSocket(PORT);
Socket socket = serverSocket.accept();
String message = getMessage(socket);
System.out.println("Server got the message: " + message);
sendResponse(socket);
}catch (IOException e){
throw new IllegalStateException(e);
}
}
private void sendResponse(Socket socket) throws IOException{
Message message = new Message("Ack");
System.out.println("Server now sending a response to the client: " + message);
OutputStream out = socket.getOutputStream();
out.write(message.toString().getBytes("UTF-8"));
}
private String getMessage(Socket socket) throws IOException{
BufferedInputStream in = new BufferedInputStream(socket.getInputStream());
StringBuilder sb = new StringBuilder(100);
byte[] bytes = new byte[1024<<8];
while(sb.lastIndexOf(Message.MSG_END) == -1){
int bytesRead = in.read(bytes);
sb.append(new String(bytes,0,bytesRead,"UTF-8"));
}
return sb.toString();
}
}
MyClient class
public class MyClient {
public static void main(String[] args){
MyClient client = new MyClient();
Thread server = new Thread(new MyServer());
server.start();
client.performCall();
}
public void performCall(){
try {
Socket socket = new Socket("127.0.0.1",MyServer.PORT);
sendMessage(socket, "Why hello there!");
System.out.println("Client got a response from the server: " + getResponse(socket));
} catch (IOException e) {
throw new IllegalStateException(e);
}
}
public String getResponse(Socket socket) throws IOException{
String response;
StringBuilder sb = new StringBuilder(100);
InputStream in = socket.getInputStream();
byte[] bytes = new byte[1024];
while(sb.lastIndexOf(Message.MSG_END) == -1){
int bytesRead = in.read(bytes);
sb.append(new String(bytes,0,bytesRead,"UTF-8"));
}
response = sb.toString();
return response;
}
public void sendMessage(Socket socket, String message) throws IOException{
Message msg = new Message(message);
System.out.println("Client now sending message to server: " + msg);
OutputStream out = socket.getOutputStream();
out.write(msg.toString().getBytes("UTF-8"));
}
}
The output
Client now sending message to server: Why hello there!
Server got the message: Why hello there!
Server now sending a response to the client: Ack
Client got a response from the server: Ack
Process finished with exit code 0
The problem is not that you are not flushing properly, but that the reading code waits for the socket to disconnect before handling the data:
while((i = in.read()) >= 0)
Will loop as long as something can be read from in (the socket's InputStream). The condition will not fail until the other peer disconnects.
Try using
socket.setTcpNoDelay(true);
There is buffering that occurs for performance reasons (read up on Nagle's algorithm).
Looking at your code it seems ok. However you are sending less than the MTU Nagle's algothrim could be holding it back until enough data is present for a full packet or you close the socket.
So - try this:
socket.setTCPNoDelay(true);
http://en.wikipedia.org/wiki/Nagle%27s_algorithm
https://docs.oracle.com/javase/8/docs/api/java/net/Socket.html#setTcpNoDelay-boolean-
Trying to work on this assignment for practice. Got stuck few with two issues.
Where should I stop the Thread after printing the request on console? Later I would need to do that after sending the response.
From where should I send the response back? I can easily do it from processRequest(). Was thinking if there is anyway to send a HttpResponse back.
Would it be ok to send the response back from HttpRequest class itself?
Code
Main class
public final class WebServer {
public static void main(String[] args) throws Exception {
int port = 1983;
final ServerSocket server = new ServerSocket(port);
System.out.println("Comes here");
Socket client = null;
while (true) {
client = server.accept();
System.out.println("Got the connection" + client.toString());
final HttpRequest request = new HttpRequest(client);
Thread thread = new Thread(request);
thread.start();
}
}
}
HttpRequest.java
final class HttpRequest implements Runnable {
Socket socket;
public HttpRequest(Socket socket) {
this.socket = socket;
}
#Override
public void run() {
try {
processRequest();
} catch (IOException e) {
e.printStackTrace();
}
}
private void processRequest() throws IOException {
String headerline = null;
DataOutputStream out = null;
BufferedReader in = null;
out = new DataOutputStream(socket.getOutputStream());
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
while ((headerline = in.readLine()).length() != 0) {
System.out.println(headerline);
}
out.close();
in.close();
socket.close();
}
}
The thread will terminate as soon as the socket is closed.
To output to the client, in this form, you must generate your own Http header that needs to be sent to the client plus all of your data that you're sending to your client. To do this, you can do:
out.writeBytes(<HttpHeaderString>);
Then for your file, you can do something like this:
FileInputStream fileToClient;
OutputStream toClient;
byte[] buffer = new byte[2048];
int bytes = 0;
while ((bytes = fileToClient.read(buffer)) != -1){
toClient.write(buffer, 0, bytes);
}
The page mentions instance of Thread class, but ideally, you don't stop threads, you return them back to the pool. Such that you don't create a new thread for every request but reuse threads.
pool = Executors.newFixedThreadPool(poolSize);
while (true) {
pool.execute(new HttpRequest(client);
}
You can do it from anywhere just keep reference to Socket's OutputStream and don't forget to flush it.
As for the naming, it's bit awkward to send response back from request object. Just rename your HttpRequest to something like HttpRequestHandler, which assumes that you'll handle incoming request here the way you prefer, and it should be fine.
I've written some serverside socket handling code and I'm concerned that potentially my packets are not always making it back to the client. I am logging all my events and in my log files it says I am sending the information. But the client is also logging events and in their logs they say they do not receive anything.
My code to send the data is as follows:
public void write(Packet packet) {
String data = packet.serialize();
log("Send=[" + data + "]", "Write"); // log to file
try {
_writer.write(data);
_writer.flush();
} catch (Exception ex) {
log(ex, "write");
}
}
Each socket is created on a new thread and I create my writers and readers immediately like so (in the public run method):
// _sockt is a Java Socket object
_writer = new BufferedWriter(new OutputStreamWriter(_socket
.getOutputStream()));
_reader = new SocketReader(_socket);
SocketReader is just a wrapper class I created for listening for responses and has a public read method like so:
public String read() throws IOException, SocketTimeoutException {
_socket.setSoTimeout(_timeOut);
if(_reader == null)
_reader = new BufferedReader(new InputStreamReader(_socket.getInputStream()));
// read from the stream
return new PacketDataInputStream(_reader).read();
}
The PacketDataInputStream wrapper class:
BufferedReader _reader = null;
public PacketDataInputStream(BufferedReader reader)
{
_reader = reader;
}
public String read() throws IOException, SocketException {
StringBuilder builder = new StringBuilder();
int c = 0;
while((c = _reader.read()) != -1)
{
char ch = (char)c;
builder.append(ch);
if(ch == PacketConstants.ETX)
break;
}
if(builder.length() > 0)
return builder.toString();
else
return null;
}
The way I'm creating the actual socket listener objects is pretty standard I think:
InetAddress address = InetAddress.getByName(IP);
server = new ServerSocket( port, 0, address);
// My own manager class to handle all the sockets connected
WebSocketManager manager = new WebSocketManager(this);
Socket connection = null;
while(bContinue)
{
connection = server.accept();
if(bContinue) {
// assign the socket to a new thread and start
// that thread
manager.newSocket(connection);
} else {
connection.close();
}
}
Is is possible that I'm using the wrong objects for sending the data back.
Should I even be using a bufferedwriter and reader? I had thought that these were the best way to go but now I'm not so sure.
It's important to note that this does not happen all the time, just sporadically. It could be the clients code having bugs but I need to make sure that I'm doing it correctly before going back to them.
This code is run on a Linux Ubuntu server. Logging occurs to a text file, nothing special there. My log files show the Send="" data going back to the client and no exception so it appears as if the .write and .flush() worked? Socket connections are persistant and only closed by the client and or network issues.
UPDATE ----- Client Side code -------:
I did manage to get some of the client side code for how they are handling the send and receiving of data (just in case it's more obvious on their end). The client is actually connecting to this server via an Android device (if that helps).
Creation of socket
static final int BUFFER_SIZE = 20000; // Maximum packet size
java.net.InetAddress server = java.net.InetAddress.getByName(url);
socket = new Socket(server, port);
// Set socket options:
socket.setReceiveBufferSize(BUFFER_SIZE);
socket.setSendBufferSize(BUFFER_SIZE);
socket.setKeepAlive(true);
socket.setTcpNoDelay(true);
Sending:
try {
// Send the packet:
OutputStream stream = socket.getOutputStream();
stream.write(p.getByteArray ());
stream.flush();
// Update the time:
lastPacketSendTime = new Date ();
} catch (IOException e) {
setError("Error sending packet (" + e.getMessage() + ")", ERROR_IO);
return false;
}
Receiving:
socket.setSoTimeout(timeout);
// Get the reader:
inputStream = socket.getInputStream();
while (true) {
// Get the next character:
int value = inputStream.read();
// Check for -1, indicating that the socket is closed:
if (value == -1) {
// The socket is closed remotely, so close it locally as well:
disconnect();
inputStream = null;
return null;
}
// ... and a bunch of other stuff to handle the actual data
}
EDIT 14-Nov:
This is actually proving to be more of a problem now. Both the client logs and the server logs appear to be sending. But at times the data doesn't appear to come through or if it does it is sometimes coming through 10 - 30 - 60 second delayed.
I can provide more information if required.
When you use BufferedReaders and BufferedWriters things get buffered. How about using the input and output streams directly.. Also, writers are character based, I don't know if you need to send binary data but if so that will be a problem with writers.
I am not sure whether this will be to your any use or not.. but i am giving you the code i used for client server communication..
Client Side:
public class ClientWala {
public static void main(String[] args) throws Exception{
Boolean b = true;
Socket s = new Socket("127.0.0.1", 4444);
System.out.println("connected: "+s.isConnected());
OutputStream output = s.getOutputStream();
PrintWriter pw = new PrintWriter(output,true);
// to write data to server
while(b){
if (!b){
System.exit(0);
}
else {
pw.write(new Scanner(System.in).nextLine());
}
}
// to read data from server
InputStream input = s.getInputStream();
InputStreamReader isr = new InputStreamReader(input);
BufferedReader br = new BufferedReader(isr);
String data = null;
while ((data = br.readLine())!=null){
// Print it using sysout, or do whatever you want with the incoming data from server
}
}
}
Server Code:
import java.io.*
import java.net.*;
public class ServerTest {
ServerSocket s;
public void go() {
try {
s = new ServerSocket(44457);
while (true) {
Socket incoming = s.accept();
Thread t = new Thread(new MyCon(incoming));
t.start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
class MyCon implements Runnable {
Socket incoming;
public MyCon(Socket incoming) {
this.incoming = incoming;
}
#Override
public void run() {
try {
PrintWriter pw = new PrintWriter(incoming.getOutputStream(),
true);
InputStreamReader isr = new InputStreamReader(
incoming.getInputStream());
BufferedReader br = new BufferedReader(isr);
String inp = null;
boolean isDone = true;
System.out.println("TYPE : BYE");
System.out.println();
while (isDone && ((inp = br.readLine()) != null)) {
System.out.println(inp);
if (inp.trim().equals("BYE")) {
System.out
.println("THANKS FOR CONNECTING...Bye for now");
isDone = false;
s.close();
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
try {
s.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
e.printStackTrace();
}
}
}
public static void main(String[] args) {
new ServerTest().go();
}
}