Hello Members,
The following Client/Server does a simple chat where I am able to send and receive one line at a time. How should I proceed if I were to send at aleast 4-5 lines at a time.
Any help (with some sample pseudocode) would be of immense help.
Thank you!
SERVER:
import java.net.*;
import java.io.*;
public class ChatServer
{ private Socket socket = null;
private ServerSocket server = null;
private DataInputStream streamIn = null;
private DataOutputStream streamOut = null;
private DataInputStream console = null;
public ChatServer(int port)
{ try
{ System.out.println("Binding to port " + port + ", please wait ...");
server = new ServerSocket(port);
System.out.println("Server started: " + server);
System.out.println("Waiting for a client ...");
socket = server.accept();
System.out.println("Client accepted: " + socket);
open();
boolean done = false;
while (!done)
{ try
{ String line = streamIn.readUTF();
System.out.println("Client says : " + line);
String line_1 = console.readLine();
System.out.println("Server says : " + line_1);
streamOut.writeUTF(line_1);
streamOut.flush();
done = line.equals("bye") || line_1.equals("bye");
}
catch(IOException ioe)
{
done = true;
}
}
close();
}
catch(IOException ioe)
{ System.out.println(ioe);
}
}
public void open() throws IOException
{ streamIn = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
console = new DataInputStream(System.in);
streamOut = new DataOutputStream(socket.getOutputStream());
}
public void close() throws IOException
{ if (socket != null) socket.close();
if (streamIn != null) streamIn.close();
if (console != null) console.close();
if (streamOut != null) streamOut.close();
}
public static void main(String args[])
{ ChatServer server = new ChatServer(2000);
}
}
CLIENT:
import java.net.*;
import java.io.*;
public class ChatServer
{ private Socket socket = null;
private ServerSocket server = null;
private DataInputStream streamIn = null;
private DataOutputStream streamOut = null;
private DataInputStream console = null;
public ChatServer(int port)
{ try
{ System.out.println("Binding to port " + port + ", please wait ...");
server = new ServerSocket(port);
System.out.println("Server started: " + server);
System.out.println("Waiting for a client ...");
socket = server.accept();
System.out.println("Client accepted: " + socket);
open();
boolean done = false;
while (!done)
{ try
{ String line = streamIn.readUTF();
System.out.println("Client says : " + line);
String line_1 = console.readLine();
System.out.println("Server says : " + line_1);
streamOut.writeUTF(line_1);
streamOut.flush();
done = line.equals("bye") || line_1.equals("bye");
}
catch(IOException ioe)
{
done = true;
}
}
close();
}
catch(IOException ioe)
{ System.out.println(ioe);
}
}
public void open() throws IOException
{ streamIn = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
console = new DataInputStream(System.in);
streamOut = new DataOutputStream(socket.getOutputStream());
}
public void close() throws IOException
{ if (socket != null) socket.close();
if (streamIn != null) streamIn.close();
if (console != null) console.close();
if (streamOut != null) streamOut.close();
}
public static void main(String args[])
{ ChatServer server = new ChatServer(2000);
}
}