dimanche 29 mars 2015

How to enable multiple clients to communicate with the server?



The problem am having is that am not sure how to enable multiple clients to communicate with the server through threading, i've attempted it but I think am doing something wrong. Any help will be appreciated.



import java.io.*;
import java.net.*;
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Client
{
JTextArea incoming;
JTextField outgoing;
BufferedReader reader;
PrintWriter writer;
Socket sock;

public static void main(String[] args)
{
Client client = new Client();
client.go();
}

public void go()
{
JFrame frame = new JFrame("Chat for class");
JPanel mainPanel = new JPanel();
incoming = new JTextArea(15,50);
incoming.setLineWrap(true);
incoming.setWrapStyleWord(true);
incoming.setEditable(false);
JScrollPane qScroller = new JScrollPane(incoming);
qScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
qScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
outgoing = new JTextField(20);
JButton sendButton = new JButton("Send");
sendButton.addActionListener(new SendButtonListener());
mainPanel.add(qScroller);
mainPanel.add(outgoing);
mainPanel.add(sendButton);
setUpNetworking();

Thread readerThread = new Thread(new IncomingReader());
readerThread.start();

frame.getContentPane().add(BorderLayout.CENTER, mainPanel);
frame.setSize(400, 500);
frame.setVisible(true);

}

private void setUpNetworking()
{
try
{
sock = new Socket("127.0.0.1", 5000);
InputStreamReader streamReader = new InputStreamReader(sock.getInputStream());
reader = new BufferedReader(streamReader);
writer = new PrintWriter(sock.getOutputStream());
System.out.println("Connection Established");

}
catch(IOException e)
{

}
}

public class SendButtonListener implements ActionListener
{

public void actionPerformed(ActionEvent arg0)
{
try
{
writer.println(outgoing.getText());
writer.flush();
}
catch(Exception ex)
{
System.out.println("Failed to send");
}

outgoing.setText("");
outgoing.requestFocus();
}
}

public class IncomingReader implements Runnable
{


public void run()
{
String message;
try
{
while((message = reader.readLine()) != null)
{
System.out.println("read" + message);
incoming.append(message + "\n");
}
}
catch(Exception x)
{

}
}
}
}




import java.io.*;
import java.net.*;
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Client
{
JTextArea incoming;
JTextField outgoing;
BufferedReader reader;
PrintWriter writer;
Socket sock;

public static void main(String[] args)
{
Client client = new Client();
client.go();
}

public void go()
{
JFrame frame = new JFrame("Chat for class");
JPanel mainPanel = new JPanel();
incoming = new JTextArea(15,50);
incoming.setLineWrap(true);
incoming.setWrapStyleWord(true);
incoming.setEditable(false);
JScrollPane qScroller = new JScrollPane(incoming);
qScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
qScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
outgoing = new JTextField(20);
JButton sendButton = new JButton("Send");
sendButton.addActionListener(new SendButtonListener());
mainPanel.add(qScroller);
mainPanel.add(outgoing);
mainPanel.add(sendButton);
setUpNetworking();

Thread readerThread = new Thread(new IncomingReader());
readerThread.start();

frame.getContentPane().add(BorderLayout.CENTER, mainPanel);
frame.setSize(400, 500);
frame.setVisible(true);

}

private void setUpNetworking()
{
try
{
sock = new Socket("127.0.0.1", 5000);
InputStreamReader streamReader = new InputStreamReader(sock.getInputStream());
reader = new BufferedReader(streamReader);
writer = new PrintWriter(sock.getOutputStream());
System.out.println("Connection Established");

}
catch(IOException e)
{

}
}

public class SendButtonListener implements ActionListener
{

public void actionPerformed(ActionEvent arg0)
{
try
{
writer.println(outgoing.getText());
writer.flush();
}
catch(Exception ex)
{
System.out.println("Failed to send");
}

outgoing.setText("");
outgoing.requestFocus();
}
}

public class IncomingReader implements Runnable
{


public void run()
{
String message;
try
{
while((message = reader.readLine()) != null)
{
System.out.println("read" + message);
incoming.append(message + "\n");
}
}
catch(Exception x)
{

}
}
}
}



Aucun commentaire:

Enregistrer un commentaire