(server)
import java.awt.BorderLayout;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class MultithreadServer extends JFrame{
private JTextArea jtaDisplay = new JTextArea();
InetAddress inetAddress;
String[] allmessage = new String[1000000];
int chatlog;
DataOutputStream outputToClient;
DataInputStream inputFromClient;
public MultithreadServer(){
jtaDisplay.setEditable(false);
add(new JScrollPane(jtaDisplay), BorderLayout.CENTER);
setTitle("Multi-Thread Server");
setSize(500,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
try{
ServerSocket serverSocket = new ServerSocket(8000);
jtaDisplay.append("Multi-Thread Server start at " + new Date() + "\n");
int clientNo = 1;
chatlog = 0;
while(true){
Socket socket = serverSocket.accept();
// Find the client's host name, and IP address
inetAddress = socket.getInetAddress();
outputToClient = new DataOutputStream(socket.getOutputStream());
inputFromClient = new DataInputStream(socket.getInputStream());
outputToClient.writeInt(chatlog);
HandleAClient task = new HandleAClient(socket);
new Thread(task).start();
clientNo++;
}
}catch(IOException ex){
System.err.println(ex);
}
}
public static void main(String[] args){
new MultithreadServer();
}
class HandleAClient implements Runnable{
private Socket socket;
public HandleAClient(Socket socket){
this.socket = socket;
}
public void run(){
try{
while(true){
String msgtoreturn = "";
int log = inputFromClient.readInt();
jtaDisplay.append( allmessage[chatlog] + "\n");
chatlog++;
outputToClient.writeInt(chatlog);
for(int i = log;i<chatlog;i++){
msgtoreturn = msgtoreturn + allmessage[i] + "\n";
}
outputToClient.writeUTF(msgtoreturn);
}
}catch(IOException ex){
System.err.println(ex);
}
}
}
}
(client)
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class Client extends JFrame{
private JTextField jtfMessage = new JTextField();
private JTextArea jtaDisplay = new JTextArea();
private DataOutputStream toServer;
private DataInputStream fromServer;
int log;
Socket socket ;
public Client(){
jtaDisplay.setEditable(false);
JPanel jp = new JPanel(new BorderLayout());
jp.add(new JLabel("Enter Message: "), BorderLayout.WEST);
jp.add(jtfMessage, BorderLayout.CENTER);
jtfMessage.setHorizontalAlignment(JTextField.RIGHT );
add(jp,BorderLayout.NORTH);
add(new JScrollPane(jtaDisplay),BorderLayout.CENTER);
setTitle("Client");
setSize(500, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
try{
socket = new Socket("localhost", 8000);
fromServer = new DataInputStream(socket.getInputStream());
toServer = new DataOutputStream(socket.getOutputStream());
log = fromServer.readInt();
}catch(IOException ex){
jtaDisplay.append(ex.toString() + "\n");
}
jtfMessage.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try{
toServer.writeInt(log);
toServer.writeUTF(jtfMessage.getText().trim());
toServer.flush();
log = fromServer.readInt();
String allmessage = fromServer.readUTF();
jtaDisplay.append(allmessage);
jtfMessage.setText("");
}catch(IOException ex){
System.err.println(ex);
}
}
});
}
public static void main (String[] args){
new Client();
}
}
the problem start when i open second client .