Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Java server code problem  (Read 3640 times)

0 Members and 1 Guest are viewing this topic.

mat123

    Topic Starter


    Hopeful

    Thanked: 16
    • Yes
    • Yes
    • Yes
  • Experience: Familiar
  • OS: Windows XP
Java server code problem
« on: April 15, 2011, 05:42:33 PM »
i have this code for a server for a program i made

Code: [Select]
package com.matter123.main;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.Socket;
import com.matter123.net.*;

public class Clienthandle implements Runnable {
Socket thesocket;
ObjectInputStream in;
ip bbs;
public Clienthandle(Socket client,ip bbs) {
thesocket=client;
this.bbs=bbs;
try {
in=new ObjectInputStream(thesocket.getInputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

@Override
public void run() {
sendable sender=null;
try {
while (true) {
while ((sender=(sendable) in.readObject()) !=null) {
System.out.println("got a object: "+sender.toString());
bbs.tell(sender);
}
}
} catch (IOException e) {
System.out.println("IOException");
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}
and
Code: [Select]
package com.matter123.main;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.*;
import java.util.ArrayList;
import java.util.Iterator;


import com.matter123.net.sendable;

public class ip implements ActionListener {
ArrayList<ObjectOutputStream> client=new ArrayList<ObjectOutputStream>();
private Beatboxserver bbs;
public ip(Beatboxserver bbs) {
this.bbs=bbs;
}
@Override
public void actionPerformed(ActionEvent arg0) {
System.out.println("got to ip.actionPerformed() with a port of"+bbs.portfield.getText());
bbs.ipport.setVisible(false);
try {
ServerSocket serversock=new ServerSocket(Integer.parseInt(bbs.portfield.getText()));
while (true) {
System.out.println("got to serversock.accept()");
Socket client=serversock.accept();
System.out.println("got a connection at "+client.getLocalAddress().getHostAddress());
ObjectOutputStream out=new ObjectOutputStream(client.getOutputStream());
this.client.add(out);
Thread t=new Thread(new Clienthandle(client, this));
t.start();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void tell(sendable sender) {
Iterator<ObjectOutputStream> it=client.iterator();
while(it.hasNext()) {
ObjectOutputStream out=it.next();
try {
out.writeObject(sender);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

}
the first time a message is received it sends the message but the second time it throws a IOException why and how do i fix it