我已经使用java套接字创建了一个聊天室应用程序,该套接字在我班级的LAN上运行良好。但是当我试图通过互联网连接它时,抛出了StreamCorruptedException。当抛出错误时,客户端和服务器都使用HSPA调制解调器连接到互联网(因为有人告诉我套接字应用程序不能通过ADSL工作)。我搜索了一个答案,不幸的是我无法解决这个问题。你能看一下吗?
这是我的代码。
1)。服务器:
import java.io.*;
import java.util.*;
import java.net.*;
public class MyServer{
ArrayList<ClientListener> ctr;
String uname;
private static MyServer instance;
public static void main(String asrgs[]){
MyServer server=MyServer.createInstance();
server.start();
}
private MyServer(){
}
public static MyServer getInstance(){
return instance;
}
public static MyServer createInstance(){
if(instance==null){
instance=new MyServer();
return instance;
}else{
System.out.println("Server is already running");
return instance;
}
}
public void broadcast(String msg, String uname){
for(int i=0;i<ctr.size();i++){
try{
if(ctr.get(i).uname!=uname){
ObjectOutputStream oostream=ctr.get(i).getOOS();
oostream.writeObject(msg+","+uname);
}
}catch(IOException ioE){
ioE.printStackTrace();
}
}
}
public void start(){
//Start Server
ctr=new ArrayList<ClientListener>();
ServerSocket myServer=null;
try{
myServer=new ServerSocket(5008);
}catch(Exception e){
System.out.println(e);
System.exit(0);
}
//accept requests
Socket socket=null;
try{
while(true){
System.out.println("Waiting for a request...");
socket=myServer.accept();
ObjectOutputStream oos=new ObjectOutputStream(socket.getOutputStream());
ObjectInputStream ois=new ObjectInputStream(socket.getInputStream());
InetAddress inet=socket.getInetAddress();
uname=inet.getHostName();
ClientListener ct=new ClientListener(oos,ois,uname);
ctr.add(ct);
new Thread(ct).start();
System.out.println("User Connected");
//new ChatRoom(oos,ois).setVisible(true);
}
}catch(IOException e){
System.out.println("No client found..");
}
}
}
2)客户:
import java.net.*;
import java.io.*;
import java.util.*;
class MyClient{
public static void main (String[] args){
Socket socket=null;
try{
socket=new Socket(args[0],5008);
System.out.println("server connected....");
ObjectInputStream ois=new ObjectInputStream(socket.getInputStream());
ObjectOutputStream oos=new ObjectOutputStream(socket.getOutputStream());
new ChatRoom(oos,ois).setVisible(true);
System.out.println("Initiated....");
}catch(IOException e){
// System.exit(0);
e.printStackTrace();
}
}
}
3)。 ClientListener:
import java.io.*;
import java.util.*;
import java.net.*;
class ClientListener implements Runnable{
private Socket socket;
ObjectOutputStream oos;
ObjectInputStream ois;
String uname;
public ClientListener(Socket socket){
this.socket=socket;
}
public ClientListener(ObjectOutputStream oos, ObjectInputStream ois, String uname){
this.oos=oos;
this.ois=ois;
this.uname=uname;
}
public void run(){
Listen();
}
private void Listen(){
while(true){
try{
String msg=(String)ois.readObject();
this.Write(msg,uname);
}catch(IOException e){
//JOptionPane.showMessageDialog(this,e.getMessage());
}catch(ClassNotFoundException e){
//JOptionPane.showMessageDialog(this,e.getMessage());
}
}
}
private void Write(String msg,String uname){
MyServer s=MyServer.getInstance();
s.broadcast(msg,uname);
}
public ObjectOutputStream getOOS(){
return this.oos;
}
public ObjectInputStream getOIS(){
return this.ois;
}
}
4)。客户聊天室(GUI):
import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.awt.event.*;
class ChatRoom extends JFrame implements Runnable{
ObjectOutputStream oos;
ObjectInputStream ois;
JTextArea textArea;
JTextField msgText;
JButton sendButton;
JMenuBar menubar;
JMenu menu;
ChatRoom(ObjectOutputStream oos, ObjectInputStream ois){
this.oos=oos;
this.ois=ois;
setSize(400,500);
setResizable(false);
setDefaultCloseOperation(3);//EXIT_ON_CLOSE
setTitle("iChat");
textArea=new JTextArea();
textArea.setEditable(false);
JScrollPane textPane=new JScrollPane(textArea);
add("Center",textPane);
JPanel southPane=new JPanel(new GridLayout(1,2));
msgText=new JTextField();
msgText.requestFocus();
msgText.addKeyListener(new KeyAdapter(){
public void keyReleased(KeyEvent evt){
if(evt.getKeyCode()==KeyEvent.VK_ENTER){
writeMessage();
}
}
});
southPane.add(msgText);
sendButton=new JButton("Send");
sendButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ect){
writeMessage();
}
});
southPane.add(sendButton);
add("South",southPane);
new Thread(this).start();
}
private void writeMessage(){
String msg=msgText.getText();
try{
oos.writeObject(msg);
textArea.append("Me : "+msg+"\n");
}catch(IOException e){
JOptionPane.showMessageDialog(this,e.getMessage());
}
msgText.setText(null);
}
public void run(){
while(true){
try{
String token=(String)ois.readObject();
int marker=token.indexOf(",");
String msg=token.substring(0,marker);
String uname=token.substring(marker);
textArea.append(uname+" : "+msg+"\n");
}catch(IOException e){
JOptionPane.showMessageDialog(this,e.getMessage());
}catch(ClassNotFoundException e){
JOptionPane.showMessageDialog(this,e.getMessage());
}
}
}
}