SOCKET编程 能够运行 但是有异常 看起来很不爽!求解决办法!!
server方代码:
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
public class EchoServer extends Frame implements ActionListener{
private int port=8000;
private ServerSocket serverSocket;
private TextArea ServerT;
private TextArea ClientT;
private Button Send;
private Button Close;
private PrintWriter pw;
Socket socket=null;
public EchoServer()throws IOException{
serverSocket=new ServerSocket(port);
setSize(400,400);
setLocation(400,200);
setLayout(new BorderLayout());
init();
Panel p1=new Panel(new GridLayout(2,1));
p1.add(ClientT);p1.add(ServerT);
Panel p=new Panel();
p.add(Send);p.add(Close);
add(p1,BorderLayout.CENTER);
add(p,BorderLayout.SOUTH);
addWindowListener(new WindowAdapter()
{public void windowClosing(WindowEvent e)
{
try{
if(socket!=null)socket.close();
}catch(IOException e1){
e1.printStackTrace();
}
System.exit(0);
}
});
setVisible(true);
}
public void init(){
ServerT=new TextArea(3,6);
ClientT=new TextArea("服务器启动",5,6);
Send=new Button("发送");
Close=new Button("关闭");
Send.addActionListener(this);
Close.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==Send){
if(pw!=null)pw.println(Serverecho(ServerT.getText()));
String str=null;
if(ClientT.getText().compareTo("")!=0)
str+=ClientT.getText()+"\n";
else
str="\n";
str=str+Serverecho(ServerT.getText());
ClientT.setText(str);
ServerT.setText("");
}
if(e.getSource()==Close){
try{
if(socket!=null)socket.close();
}catch(IOException e1){
e1.printStackTrace();
}
}
}
public String Serverecho(String msg){
return "静夜:"+msg;
}
private PrintWriter getWriter(Socket socket)throws IOException{
OutputStream socketOut=socket.getOutputStream();
return new PrintWriter(socketOut,true);
}
private BufferedReader getReader(Socket socket)throws IOException{
InputStream socketIn=socket.getInputStream();
return new BufferedReader(new InputStreamReader(socketIn));
}
public void service(){
while(true){
try{
socket=serverSocket.accept();
String str="New Connection accepted"
+socket.getInetAddress()+":"+socket.getPort();
ClientT.setText(ClientT.getText()+"\n"+str);
BufferedReader br=getReader(socket);
pw=getWriter(socket);
String msg=null;
while((msg=br.readLine())!=null){
ClientT.setText(ClientT.getText()+"\n"+msg);
}
}
catch(IOException e){
e.printStackTrace();
}
}
}
public static void main(String args[])throws IOException{
new EchoServer().service();
}
}
client方 代码:
import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
public class EchoClient extends Frame implements ActionListener{
private String host="localhost";
private int port=8000;
private Socket socket;
private TextArea ClientT;
private TextArea ServerT;
private Button Send;
private Button Close;
private PrintWriter pw;
public EchoClient()throws IOException{
socket=new Socket(host,port);
init();
setSize(400,400);
setLocation(600,200);
setLayout(new BorderLayout());
Panel p1=new Panel(new GridLayout(2,1));
p1.add(ServerT);p1.add(ClientT);
Panel p=new Panel();
p.add(Send);p.add(Close);
add(p1,BorderLayout.CENTER);
add(p,BorderLayout.SOUTH);
addWindowListener(new WindowAdapter()
{public void windowClosing(WindowEvent e)
{
try{
if(socket!=null)socket.close();
}catch(IOException e1){
e1.printStackTrace();
}
System.exit(0);
}
});
setVisible(true);
}
public void init(){
ClientT=new TextArea(3,6);
ServerT=new TextArea(5,6);
Send=new Button("发送");
Close=new Button("关闭");
Send.addActionListener(this);
Close.addActionListener(this);
try{
pw=getWriter(socket);
}
catch(IOException e){
e.printStackTrace();
}
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==Send){
pw.println(Clientcho(ClientT.getText()));
String str=null;
if(ServerT.getText().compareTo("")!=0)
str+=ServerT.getText()+"\n";
else
str="\n";
str=str+Clientcho(ClientT.getText());
ServerT.setText(str);
ClientT.setText("");
}
if(e.getSource()==Close){
try{
if(socket!=null)socket.close();
}catch(IOException e1){
e1.printStackTrace();
}
}
}
public String Clientcho(String msg){
return "一生有你:"+msg;
}
public static void main(String args[])throws IOException{
new EchoClient().talk();
}
private PrintWriter getWriter(Socket socket)throws IOException{
OutputStream socketOut=socket.getOutputStream();
return new PrintWriter(socketOut,true);
}
private BufferedReader getReader(Socket socket)throws IOException{
InputStream socketIn=socket.getInputStream();
return new BufferedReader(new InputStreamReader(socketIn));
}
public void talk()throws IOException{
while(true){
try{
BufferedReader br=getReader(socket);
String msg=null;
while((msg=br.readLine())!=null){
ServerT.setText(ServerT.getText()+"\n"+msg);
}
}
catch(IOException e){
e.printStackTrace();
}
}
}
}