Results 1 to 2 of 2

Thread: Exception!

  1. #1
    rameshraj is offline Member
    Join Date
    Dec 2007
    Posts
    42
    Rep Power
    0

    Thumbs up Exception!

    java.io.StreamCorruptedException: invalid stream header: 456E7465
    java.io.StreamCorruptedException: invalid stream header: 49

    I got the above two exceptions when I run my chat application.When the client sends the message then the above two exceptions come and the applications stops to respond.
    I have three classes:ChatBoard.java which lists the sample users.ChatSingle.java is the single thread of communication between the server and the client.
    The MultiThreadChatServer does the working of the server.Here are the codes:

    >>>>>>>>>>>>>>>>>>ChatBoard.java:

    public class ChatBoard extends javax.swing.JFrame {

    /** Creates new form ChatBoard */
    public ChatBoard() {
    setBounds(300,300,500,500);
    setTitle("DAS Chat System");
    initComponents();
    }

    /** This method is called from within the constructor to
    * initialize the form.
    * WARNING: Do NOT modify this code. The content of this method is
    * always regenerated by the Form Editor.
    */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

    jPanel1 = new javax.swing.JPanel();
    jScrollPane2 = new javax.swing.JScrollPane();
    listOnlineUsers = new javax.swing.JList();
    jScrollPane1 = new javax.swing.JScrollPane();
    taCollectedMsg = new javax.swing.JTextArea();
    jPanel2 = new javax.swing.JPanel();
    tfMsg = new javax.swing.JTextField();
    btnSendMsg = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstan ts.EXIT_ON_CLOSE);
    setBounds(new java.awt.Rectangle(300, 300, 300, 300));

    listOnlineUsers.setModel(new javax.swing.AbstractListModel() {
    String[] strings = onlineUsers;
    public int getSize() { return strings.length; }
    public Object getElementAt(int i) { return strings[i]; }
    });
    listOnlineUsers.setToolTipText("Another ip is 192.168.2.75");
    listOnlineUsers.addMouseListener(new java.awt.event.MouseAdapter() {
    public void mouseClicked(java.awt.event.MouseEvent evt) {
    listOnlineUsersMouseClicked(evt);
    }
    });
    jScrollPane2.setViewportView(listOnlineUsers);

    taCollectedMsg.setColumns(20);
    taCollectedMsg.setRows(5);
    jScrollPane1.setViewportView(taCollectedMsg);

    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(
    jPanel1Layout.createParallelGroup(javax.swing.Grou pLayout.Alignment.LEADING)
    .addGroup(jPanel1Layout.createSequentialGroup()
    .addContainerGap()
    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 326, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED, 31, Short.MAX_VALUE)
    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 219, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addContainerGap())
    );
    jPanel1Layout.setVerticalGroup(
    jPanel1Layout.createParallelGroup(javax.swing.Grou pLayout.Alignment.LEADING)
    .addGroup(jPanel1Layout.createSequentialGroup()
    .addGroup(jPanel1Layout.createParallelGroup(javax. swing.GroupLayout.Alignment.LEADING)
    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 154, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE))
    .addContainerGap(16, Short.MAX_VALUE))
    );

    btnSendMsg.setText("SEND");
    btnSendMsg.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    btnSendMsgActionPerformed(evt);
    }
    });

    javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
    jPanel2.setLayout(jPanel2Layout);
    jPanel2Layout.setHorizontalGroup(
    jPanel2Layout.createParallelGroup(javax.swing.Grou pLayout.Alignment.LEADING)
    .addGroup(jPanel2Layout.createSequentialGroup()
    .addGroup(jPanel2Layout.createParallelGroup(javax. swing.GroupLayout.Alignment.LEADING)
    .addGroup(jPanel2Layout.createSequentialGroup()
    .addContainerGap()
    .addComponent(tfMsg, javax.swing.GroupLayout.PREFERRED_SIZE, 310, javax.swing.GroupLayout.PREFERRED_SIZE))
    .addGroup(jPanel2Layout.createSequentialGroup()
    .addGap(27, 27, 27)
    .addComponent(btnSendMsg)))
    .addContainerGap(javax.swing.GroupLayout.DEFAULT_S IZE, Short.MAX_VALUE))
    );
    jPanel2Layout.setVerticalGroup(
    jPanel2Layout.createParallelGroup(javax.swing.Grou pLayout.Alignment.LEADING)
    .addGroup(jPanel2Layout.createSequentialGroup()
    .addContainerGap()
    .addComponent(tfMsg, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED, 26, Short.MAX_VALUE)
    .addComponent(btnSendMsg))
    );

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addGap(10, 10, 10)
    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    .addGroup(layout.createSequentialGroup()
    .addGap(20, 20, 20)
    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
    );
    layout.setVerticalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addGap(30, 30, 30)
    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addGap(30, 30, 30)
    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
    );

    pack();
    }// </editor-fold>

    private void listOnlineUsersMouseClicked(java.awt.event.MouseEv ent evt) {

    if(evt.getClickCount()==2)
    {
    String value=listOnlineUsers.getSelectedValue().toString( );
    System.out.println("Selected user is\t"+value+"and port is\t"+port);
    chatSingle=new ChatSingle(value, port);
    chatSingle.setVisible(true);



    }
    }

    private void btnSendMsgActionPerformed(java.awt.event.ActionEve nt evt) {
    MultiThreadChatClient chatClient=new MultiThreadChatClient("localhost",12345);
    chatClient.setMsg(tfMsg.getText());
    chatClient.setCollectMsg(taCollectedMsg.getText(). concat("\n"+tfMsg.getText()));

    taCollectedMsg.setText(taCollectedMsg.getText().co ncat("\n"+tfMsg.getText()));
    tfMsg.setText("");
    //chatClient.runClient();

    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
    public void run() {
    new ChatBoard().setVisible(true);
    //chatServer=new MultiThreadChatServer();
    //chatServer.runServer();

    }
    });
    }

    // Variables declaration - do not modify
    private javax.swing.JButton btnSendMsg;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JList listOnlineUsers;
    private javax.swing.JTextArea taCollectedMsg;
    private javax.swing.JTextField tfMsg;
    // End of variables declaration
    private String []onlineUsers={"192.168.2.23","192.168.2.75","127.0. 0.1","localhost"};
    private static MultiThreadChatClient chatClient;
    private static MultiThreadChatServer chatServer;
    private String host="";
    private int port=12345;
    private ChatSingle chatSingle;
    }


    >>>>>>>>>>>>>>>>>>>>>>>ChatSingle.java:
    >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    /*
    * ChatSingle.java
    *
    * Created on May 5, 2008, 12:11 PM
    */

    package attendance.MultithreadChat;

    import java.io.BufferedReader;
    import java.io.DataInputStream;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.io.PrintStream;
    import java.net.Socket;
    import java.net.UnknownHostException;

    /**
    *
    * @author ramesh
    */
    public class ChatSingle extends javax.swing.JFrame {
    private String host;

    /** Creates new form ChatSingle */
    public ChatSingle(String host,int port) {
    this.hostName=host;
    this.port=port;
    setBounds(200,200,300, 300);

    initComponents();
    }

    /** This method is called from within the constructor to
    * initialize the form.
    * WARNING: Do NOT modify this code. The content of this method is
    * always regenerated by the Form Editor.
    */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

    jPanel1 = new javax.swing.JPanel();
    jScrollPane1 = new javax.swing.JScrollPane();
    taCollectedMsg = new javax.swing.JTextArea();
    jPanel2 = new javax.swing.JPanel();
    tfMsg = new javax.swing.JTextField();
    btnSendMsg = new javax.swing.JButton();
    jMenuBar1 = new javax.swing.JMenuBar();
    jMenu1 = new javax.swing.JMenu();
    jMenuItem1 = new javax.swing.JMenuItem();
    jMenuItem2 = new javax.swing.JMenuItem();
    jMenu2 = new javax.swing.JMenu();
    jMenu3 = new javax.swing.JMenu();
    jMenuItem3 = new javax.swing.JMenuItem();
    jMenuItem4 = new javax.swing.JMenuItem();
    jMenuItem5 = new javax.swing.JMenuItem();

    setDefaultCloseOperation(javax.swing.WindowConstan ts.DISPOSE_ON_CLOSE);

    taCollectedMsg.setColumns(20);
    taCollectedMsg.setRows(5);
    jScrollPane1.setViewportView(taCollectedMsg);

    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(
    jPanel1Layout.createParallelGroup(javax.swing.Grou pLayout.Alignment.LEADING)
    .addGroup(jPanel1Layout.createSequentialGroup()
    .addContainerGap(javax.swing.GroupLayout.DEFAULT_S IZE, Short.MAX_VALUE)
    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 490, javax.swing.GroupLayout.PREFERRED_SIZE))
    );
    jPanel1Layout.setVerticalGroup(
    jPanel1Layout.createParallelGroup(javax.swing.Grou pLayout.Alignment.LEADING)
    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 167, Short.MAX_VALUE)
    );

    tfMsg.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    tfMsgActionPerformed(evt);
    }
    });

    btnSendMsg.setText("SEND");
    btnSendMsg.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    btnSendMsgActionPerformed(evt);
    }
    });

    javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
    jPanel2.setLayout(jPanel2Layout);
    jPanel2Layout.setHorizontalGroup(
    jPanel2Layout.createParallelGroup(javax.swing.Grou pLayout.Alignment.LEADING)
    .addGroup(jPanel2Layout.createSequentialGroup()
    .addGroup(jPanel2Layout.createParallelGroup(javax. swing.GroupLayout.Alignment.LEADING)
    .addGroup(jPanel2Layout.createSequentialGroup()
    .addContainerGap()
    .addComponent(tfMsg, javax.swing.GroupLayout.DEFAULT_SIZE, 489, Short.MAX_VALUE))
    .addGroup(jPanel2Layout.createSequentialGroup()
    .addGap(18, 18, 18)
    .addComponent(btnSendMsg)))
    .addContainerGap())
    );
    jPanel2Layout.setVerticalGroup(
    jPanel2Layout.createParallelGroup(javax.swing.Grou pLayout.Alignment.LEADING)
    .addGroup(jPanel2Layout.createSequentialGroup()
    .addContainerGap()
    .addComponent(tfMsg, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED, 28, Short.MAX_VALUE)
    .addComponent(btnSendMsg))
    );

    jMenu1.setText("File");

    jMenuItem1.setText("Save Logs");
    jMenu1.add(jMenuItem1);

    jMenuItem2.setText("Exit");
    jMenu1.add(jMenuItem2);

    jMenuBar1.add(jMenu1);

    jMenu2.setText("Edit");

    jMenu3.setText("Font");

    jMenuItem3.setText("Size");
    jMenu3.add(jMenuItem3);

    jMenuItem4.setText("Color");
    jMenu3.add(jMenuItem4);

    jMenuItem5.setText("Type");
    jMenu3.add(jMenuItem5);

    jMenu2.add(jMenu3);

    jMenuBar1.add(jMenu2);

    setJMenuBar(jMenuBar1);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.LEADING)
    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
    .addContainerGap(31, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addGap(27, 27, 27)
    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addGap(18, 18, 18)
    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addContainerGap(16, Short.MAX_VALUE))
    );

    pack();
    }// </editor-fold>

    private void tfMsgActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:
    }

    private void btnSendMsgActionPerformed(java.awt.event.ActionEve nt evt) {

    this.message=tfMsg.getText();
    this.collectedMsg=taCollectedMsg.getText()+"\nMe:\ t"+tfMsg.getText();
    taCollectedMsg.setText(collectedMsg);
    tfMsg.setText("");
    // Try to open a socket on a given host and port
    // Try to open input and output streams
    try {
    clientSocket = new Socket(hostName,port);
    //inputLine = new BufferedReader(new InputStreamReader(System.in));
    //os = new PrintStream(clientSocket.getOutputStream());
    //is = new DataInputStream(clientSocket.getInputStream());
    System.out.println("inside1");
    oos=new ObjectOutputStream(clientSocket.getOutputStream()) ;
    System.out.println("inside2");
    oos.writeBytes("Hi there");
    oos.flush();
    ois=new ObjectInputStream(clientSocket.getInputStream());
    //oos.writeBytes(tfMsg.getText());
    System.out.println("client got\t"+ois.readLine().toString());

    } catch (UnknownHostException e) {
    System.err.println("Don't know about host "+hostName);
    }
    catch (IOException e)
    {
    System.err.println("Couldn't get I/O for the connection to the host "+hostName+"\t"+e);
    }
    if (clientSocket != null && os != null && is != null) {
    try {

    // Create a thread to read from the server
    //new Thread(new MultiThreadChatClient(hostName,port)).start();

    while (!closed) {
    os.println(tfMsg.getText());
    }

    // Clean up:
    // close the output stream
    // close the input stream
    // close the socket

    //os.close();
    //is.close();
    //clientSocket.close();
    } catch (Exception e) {
    System.err.println("Exception: " + e);
    }
    }
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
    public void run() {
    new ChatSingle("locahost",12345).setVisible(true);
    }
    });
    }

    // Variables declaration - do not modify
    private javax.swing.JButton btnSendMsg;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JMenuItem jMenuItem4;
    private javax.swing.JMenuItem jMenuItem5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea taCollectedMsg;
    private javax.swing.JTextField tfMsg;
    // End of variables declaration
    private static String hostName="";
    private static int port=12345;
    private static String message="";
    private static String collectedMsg="";
    private static MultiThreadChatClient chatClient;
    private static MultiThreadChatServer chatServer;
    static Socket clientSocket = null;
    static PrintStream os = null;
    static DataInputStream is = null;
    static BufferedReader inputLine = null;
    static boolean closed = false;
    private static ObjectInputStream ois;
    private static ObjectOutputStream oos;
    }

    >>>>>>>>>>>>>>>>>>>>>>>>>>>MultiThreadChatServer.j ava
    >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */

    package attendance.MultithreadChat;

    /**
    *
    * @author ramesh
    */
    import java.io.*;
    import java.net.*;

    public class MultiThreadChatServer{

    // Declaration section:
    // declare a server socket and a client socket for the server
    // declare an input and an output stream

    static Socket clientSocket = null;
    static ServerSocket serverSocket = null;
    private static ObjectInputStream objectInput;
    private static ObjectOutputStream objectOutput;

    // This chat server can accept up to 2 clients' connections

    static clientThread t[] = new clientThread[10];

    public static void main(String args[]) {

    // The default port
    int port_number=12345;
    try {
    serverSocket = new ServerSocket(port_number);
    }
    catch (IOException e)
    {System.out.println("Exception1 in main in server"+e);}

    // Create a socket object from the ServerSocket to listen and accept
    // connections.
    // Open input and output streams for this socket will be created in
    // client's thread since every client is served by the server in
    // an individual thread

    while(true){

    try {
    clientSocket = serverSocket.accept();
    for(int i=0; i<=9; i++){
    if(t[i]==null)
    {
    (
    t[i] = new clientThread(clientSocket,t)).start();
    break;
    }

    }
    //System.out.println("Hello u are welcome\t"+t[0].activeCount()+clientSocket.getInetAddress());
    DataInputStream is = null;
    PrintStream os = null;//alternatively dataoutput stream
    is = new DataInputStream(clientSocket.getInputStream());
    os = new PrintStream(clientSocket.getOutputStream());

    objectOutput=new ObjectOutputStream(clientSocket.getOutputStream()) ;
    os.println("Hello Client\t"+clientSocket.getInetAddress());
    objectOutput.writeBytes("hello client");
    objectOutput.flush();
    objectInput=new ObjectInputStream(clientSocket.getInputStream());
    System.out.println("Client has sent:"+objectInput.readChar());
    for(int i=0;i<t[0].activeCount();i++)
    {
    System.out.println("Name of thread \t"+t[i].getName()+t[i].getId());


    }

    }
    catch (IOException e) {
    System.out.println("Exception2 in server"+e);}
    catch(Exception ex)
    {
    System.out.println("Exception3 in server"+ex);
    }
    }
    }
    public void runServer()
    {
    // The default port
    int port_number=12345;
    try {
    serverSocket = new ServerSocket(port_number);
    }
    catch (IOException e)
    {System.out.println("Exception1 in runServer\t"+e);}

    // Create a socket object from the ServerSocket to listen and accept
    // connections.
    // Open input and output streams for this socket will be created in
    // client's thread since every client is served by the server in
    // an individual thread

    while(true){

    try {
    clientSocket = serverSocket.accept();
    for(int i=0; i<=9; i++){
    if(t[i]==null)
    {
    (
    t[i] = new clientThread(clientSocket,t)).start();
    break;
    }

    }
    //System.out.println("Hello u are welcome\t"+t[0].activeCount()+clientSocket.getInetAddress());
    DataInputStream is = null;
    PrintStream os = null;//alternatively dataoutput stream
    is = new DataInputStream(clientSocket.getInputStream());
    os = new PrintStream(clientSocket.getOutputStream());
    os.println("Hello Client\t"+clientSocket.getInetAddress());
    for(int i=0;i<t[0].activeCount();i++)
    {
    System.out.println("Name of thread \t"+t[i].getName()+t[i].getId());


    }

    }
    catch (IOException e) {
    System.out.println("Exception2 in runServer\t"+e);}
    catch(Exception ex)
    {
    System.out.println("Exception3 in runServer\t"+ex);
    }
    }
    }
    }

    // This client thread opens the input and the output streams for a particular client,
    // ask the client's name, informs all the clients currently connected to the
    // server about the fact that a new client has joined the chat room,
    // and as long as it receive data, echos that data back to all other clients.
    // When the client leaves the chat room this thread informs also all the
    // clients about that and terminates.

    class clientThread extends Thread{

    DataInputStream is = null;
    PrintStream os = null;//alternatively dataoutput stream
    Socket clientSocket = null;
    clientThread t[];
    ObjectInputStream objectInputStream;
    ObjectOutputStream objectOutputStream;

    public clientThread(Socket clientSocket, clientThread[] t){
    this.clientSocket=clientSocket;
    this.t=t;
    }

    public void run()
    {
    String line;
    String name;
    try{
    is = new DataInputStream(clientSocket.getInputStream());
    os = new PrintStream(clientSocket.getOutputStream());
    os.println("Enter your name.");

    name = is.readLine();
    os.println("Hello "+name+" Welcome to our chat room.\nTo leave enter /quit in a new line");
    objectOutputStream=new ObjectOutputStream(clientSocket.getOutputStream()) ;
    objectOutputStream.writeBytes("This is me server");
    objectOutputStream.flush();
    objectInputStream=new ObjectInputStream(clientSocket.getInputStream());
    System.out.println("Client sent:"+objectInputStream.readLine());
    //this is the multicast message sent by the server to all of the clients
    for(int i=0; i<=9; i++)
    if (t[i]!=null && t[i]!=this)
    t[i].os.println("*** A new user "+name+" entered the chat room !!! ***" );
    while (true) {
    line = is.readLine();
    System.out.println("to me:\t"+line+"from\t"+currentThread().getName()+"\t "+currentThread().getId());
    int id=Integer.parseInt(currentThread().getName().spli t("-")[1]);
    t[id].os.println("U were the current thread");
    if(line.startsWith("/quit")) break;
    for(int i=0; i<=9; i++)
    if (t[i]!=null) t[i].os.println("<"+name+"> "+line);
    }
    for(int i=0; i<=9; i++)
    if (t[i]!=null && t[i]!=this)
    t[i].os.println("*** The user "+name+" is leaving the chat room !!! ***" );

    os.println
    ("*** Bye "+name+" ***");

    // Clean up:
    // Set to null the current thread variable such that other client could
    // be accepted by the server
    for(int i=0; i<=9; i++)
    if (t[i]==this) t[i]=null;
    // close the output stream
    // close the input stream
    // close the socket
    is.close();
    os.close();
    clientSocket.close();
    }
    catch(IOException e){
    System.out.println("IOExcep in run of server"+e);
    }
    catch(NullPointerException npe)
    {
    System.out.println("NPE in server's run\t"+npe);
    }
    }

    }

  2. #2
    rameshraj is offline Member
    Join Date
    Dec 2007
    Posts
    42
    Rep Power
    0

    Thumbs up

    While running run the server program first and then the ChatBoard.java.

Similar Threads

  1. Need help on Exception
    By Deon in forum New To Java
    Replies: 7
    Last Post: 02-11-2010, 06:46 PM
  2. Where does the exception go?
    By aytidaalkuhs in forum New To Java
    Replies: 3
    Last Post: 04-07-2008, 03:24 PM
  3. exception
    By Oktam in forum New To Java
    Replies: 2
    Last Post: 03-23-2008, 08:01 PM
  4. Replies: 1
    Last Post: 03-08-2008, 07:41 PM
  5. Exception
    By Camden in forum New To Java
    Replies: 2
    Last Post: 11-27-2007, 12:50 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •