Results 1 to 3 of 3
Thread: multithreading server / client
- 03-06-2011, 03:29 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 4
- Rep Power
- 0
multithreading server / client
Hi all,this is my first post in this forum ,I want to create server that handle more multi clients simulitenusly ,so the clients write in the stream and the text appear in the server console ,the previeus code is implemented and no problem for it.but I want that the server when arrived a messege from client it must convert it to capital letter (string.touppercaset) then re-send it to the client whose sent the message to the server
ChatServer.java
Java Code:package servergu; import java.net.*; import java.io.*; public class ChatServer implements Runnable { private ServerSocket server = null; private Thread thread = null; private ChatServerThread client = 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); start(); } catch (IOException ioe) { System.out.println(ioe); } } public void run() { while (thread != null) { try { System.out.println("Waiting for a client ..."); addThread(server.accept()); } catch (IOException ie) { System.out.println("Acceptance Error: " + ie); } } } public void addThread(Socket socket) { System.out.println("Client accepted: " + socket); client = new ChatServerThread(this, socket); try { client.open(); client.start(); } catch (Exception ioe) { System.out.println("Error opening thread: " + ioe); } } public void start() { if (thread == null) { thread = new Thread(this); thread.start(); } } public void stop() { if (thread != null) { thread.stop(); thread = null; } } public static void main(String args[]) { ChatServer ch = new ChatServer(9999); } }
ChatServerThread.java
Java Code:package servergu; import java.net.*; import java.io.*; public class ChatServerThread extends Thread { private Socket socket = null; private ChatServer server = null; private int ID = -1; private DataInputStream streamIn = null; public ChatServerThread(ChatServer _server, Socket _socket) { server = _server; socket = _socket; ID = socket.getPort(); } public void run() { System.out.println("Server Thread " + ID + " running."); while (true) { try { System.out.println(streamIn.readUTF()); } catch(IOException ioe) { } } } public void open() throws IOException { streamIn = new DataInputStream(new BufferedInputStream(socket.getInputStream())); } public void close() throws IOException { if (socket != null) socket.close(); if (streamIn != null) streamIn.close(); } }
and this is client's code:
thanx in advanceJava Code:package tt1; //import com.sun.corba.se.spi.activation.Server; import java.awt.Color; import java.awt.GridLayout; import java.awt.List; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.net.*; import java.io.*; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextField; public class Client { private static List list1; private static JTextField field; public static String Servername; public static int ServerPort; public Client() { } public Client(String serverName, int serverPort) throws IOException { this.Servername = serverName; this.ServerPort = serverPort; } static Socket socket = null; //static DataInputStream console = null; static DataOutputStream streamOut = null; public static void main(String args[]) throws IOException { JFrame f = new JFrame(); JPanel panel = new JPanel(new GridLayout(2, 1, 20, 50)); f.setSize(500, 400); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setName( "mainPanel"); // NOI18N list1 = new java.awt.List(); list1.setSize( 200, 200); list1.setName( "list1"); // NOI18N field = new JTextField(); field.setSize( 100, 200); panel.add(list1); panel.add(field); JButton button1 = new JButton("Send"); button1.setSize(50, 100); panel.add(button1); f.add(panel); f.setVisible( true); System.out.println("Establishing connection. Please wait ..."); try { Client client = new Client("BilalPC", 9999); socket = new Socket(Servername, ServerPort); System.out.println("Connected: " + socket); //console = new DataInputStream(System.in); streamOut = new DataOutputStream(socket.getOutputStream()); } catch (UnknownHostException uhe) { System.out.println("Host unknown: " + uhe.getMessage()); } catch (IOException ioe) { System.out.println("Unexpected exception: " + ioe.getMessage()); } button1.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { //Execute when button is pressed try { String line = field.getText(); if (!line.equals("bye")) { try { list1.add(line); streamOut.writeUTF(line); streamOut.flush(); } catch (IOException ioe) { System.out.println("Sending error: " + ioe.getMessage()); } } } catch (Exception ex) { Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex); } } }); } }
- 03-06-2011, 03:31 PM #2
Member
- Join Date
- Mar 2011
- Posts
- 4
- Rep Power
- 0
I am Just wondering what is the code lines that I have to Edit or change
-
Please see an important private message that I've sent you.
Similar Threads
-
multithreading server / client
By abulwleed in forum NetworkingReplies: 2Last Post: 03-07-2011, 04:37 PM -
server-client; client sends a username to the server.
By lkcz in forum New To JavaReplies: 2Last Post: 09-24-2010, 11:31 AM -
Datagram Client and Server, client timer question
By saru88 in forum NetworkingReplies: 1Last Post: 10-05-2008, 03:12 PM -
Identify Client in Socket Client Server Application
By masadjie in forum NetworkingReplies: 1Last Post: 12-20-2007, 09:18 AM -
socket Multithreading - & - Obtaining the IP of a client!
By bluebarca in forum NetworkingReplies: 1Last Post: 11-16-2007, 10:09 AM


LinkBack URL
About LinkBacks

Bookmarks