客户端先启动 再启动服务器的时候有个异常 不知道怎么解决
还有就是客户端第一次发信息也有抛出个异常 信息发不出去
再就是我定义了关毕窗口的时候就关闭通讯类 可是就是关不掉 而是抛出异常 谁帮我看看
//服务器端
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import java.util.*;
public class Server extends JFrame{
JTextArea txt;
ServerSocket seSocket=null;
public void serve(){
txt=new JTextArea();
this.setTitle("学习通讯类");
this.setLayout(new BorderLayout());
this.add(txt,BorderLayout.CENTER);
this.setSize(500,300);
this.setVisible(true);
try{
seSocket=new ServerSocket(8000);
txt.append("服务器启动时间是:"+new Date()+'\n');
} catch (BindException e) {
System.out.println("端口使用中....");
System.out.println("请关掉相关程序并重新运行服务器!");
System.exit(0);
} catch (IOException e) {}
try{
while(true){
Socket st=seSocket.accept();
CtThread ct=new CtThread();
ct.deliveSt(st);
new Thread(ct).start();
}
}catch(IOException e){}
}
class CtThread implements Runnable{
Socket st=null;
DataInputStream in=null;
public void deliveSt(Socket st)
{
this.st=st;
try{
in=new DataInputStream(st.getInputStream());
}catch(IOException e){}
}
public void run()
{
try{
BufferedReader br=new BufferedReader(new InputStreamReader(in));
while(true){
String Dialog=br.readLine();
txt.append("从客户端收到信息"+Dialog+'\n');
}
}catch(IOException e){}
}
}
public static void main(String args[])
{
Server myserver=new Server();
myserver.serve();
}
}//客户端类import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import java.util.*;public class Client extends JFrame implements ActionListener
{
JTextArea txta;
JTextField txtf;
JPanel pl;
JButton bt;
DataOutputStream out=null;
Socket sc=null;
PrintStream ps;
Container f=this.getContentPane();
public void client()
{
f.setLayout(new BorderLayout());
txta=new JTextArea();
f.add(txta,BorderLayout.CENTER);
txtf=new JTextField(20);
bt=new JButton("发送");
pl=new JPanel();
pl.setLayout(new FlowLayout());
pl.add(txtf);
pl.add(bt);
bt.addActionListener(this);
txtf.addActionListener(this);
f.add(pl,BorderLayout.SOUTH);
setTitle("信息发送端");
setSize(500,300);
setVisible(true);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
disconnect();
System.exit(0);
}
});
try
{
Socket sc=new Socket("192.168.0.25",8000);
out=new DataOutputStream(sc.getOutputStream());
ps=new PrintStream(out);
}
catch(IOException ex)
{
txta.append(ex.toString()+'\n');
}
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==bt|e.getSource()==txtf)
{
String Dialog=txtf.getText();
try
{
ps.println(Dialog);
txta.append("已经发送对话:"+Dialog+'\n');
txtf.setText("");
}
catch(Exception ex)
{
txta.append(ex.toString()+'\n');
}
}
}
public void disconnect() {
try {
out.close();
sc.close();
} catch (IOException e) {}
}
public static void main(String args[])
{
Client myclient=new Client();
myclient.client();
}
}谁帮改改
还有就是客户端第一次发信息也有抛出个异常 信息发不出去
再就是我定义了关毕窗口的时候就关闭通讯类 可是就是关不掉 而是抛出异常 谁帮我看看
//服务器端
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import java.util.*;
public class Server extends JFrame{
JTextArea txt;
ServerSocket seSocket=null;
public void serve(){
txt=new JTextArea();
this.setTitle("学习通讯类");
this.setLayout(new BorderLayout());
this.add(txt,BorderLayout.CENTER);
this.setSize(500,300);
this.setVisible(true);
try{
seSocket=new ServerSocket(8000);
txt.append("服务器启动时间是:"+new Date()+'\n');
} catch (BindException e) {
System.out.println("端口使用中....");
System.out.println("请关掉相关程序并重新运行服务器!");
System.exit(0);
} catch (IOException e) {}
try{
while(true){
Socket st=seSocket.accept();
CtThread ct=new CtThread();
ct.deliveSt(st);
new Thread(ct).start();
}
}catch(IOException e){}
}
class CtThread implements Runnable{
Socket st=null;
DataInputStream in=null;
public void deliveSt(Socket st)
{
this.st=st;
try{
in=new DataInputStream(st.getInputStream());
}catch(IOException e){}
}
public void run()
{
try{
BufferedReader br=new BufferedReader(new InputStreamReader(in));
while(true){
String Dialog=br.readLine();
txt.append("从客户端收到信息"+Dialog+'\n');
}
}catch(IOException e){}
}
}
public static void main(String args[])
{
Server myserver=new Server();
myserver.serve();
}
}//客户端类import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import java.util.*;public class Client extends JFrame implements ActionListener
{
JTextArea txta;
JTextField txtf;
JPanel pl;
JButton bt;
DataOutputStream out=null;
Socket sc=null;
PrintStream ps;
Container f=this.getContentPane();
public void client()
{
f.setLayout(new BorderLayout());
txta=new JTextArea();
f.add(txta,BorderLayout.CENTER);
txtf=new JTextField(20);
bt=new JButton("发送");
pl=new JPanel();
pl.setLayout(new FlowLayout());
pl.add(txtf);
pl.add(bt);
bt.addActionListener(this);
txtf.addActionListener(this);
f.add(pl,BorderLayout.SOUTH);
setTitle("信息发送端");
setSize(500,300);
setVisible(true);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
disconnect();
System.exit(0);
}
});
try
{
Socket sc=new Socket("192.168.0.25",8000);
out=new DataOutputStream(sc.getOutputStream());
ps=new PrintStream(out);
}
catch(IOException ex)
{
txta.append(ex.toString()+'\n');
}
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==bt|e.getSource()==txtf)
{
String Dialog=txtf.getText();
try
{
ps.println(Dialog);
txta.append("已经发送对话:"+Dialog+'\n');
txtf.setText("");
}
catch(Exception ex)
{
txta.append(ex.toString()+'\n');
}
}
}
public void disconnect() {
try {
out.close();
sc.close();
} catch (IOException e) {}
}
public static void main(String args[])
{
Client myclient=new Client();
myclient.client();
}
}谁帮改改
解决方案 »
- 使用paint()方法重绘的问题
- 紧急需要----需要JSP英文内容最好是有英文和汉语双份译过来的.......
- 求教:怎么样判段一个字符串是否满足yyyymmddhhmiss格式的日期?
- 招Java人才,工作地点:北京
- 新手问题 关于多态的小问题,请进,谢谢
- 我想用UML描述一个文件系统,想征集一些构思!
- 我的Jcreator2.5 pro不能显示和识别中文和日文,怎么办?
- 在jBuilder7内如何配Inprise Application Server?
- 哪有JBuilder7下载?
- 我是一个java菜鸟,想问一下什么是构建器
- java模拟电信月末计费的小程序(那位高手帮忙看看错在那里啊?)
- Java面试小程序,没做出来.....
Thinking in java 中的通讯例子,你参考下吧//:MultiJabberServer.java
import java.io.*;
import java.net.*;class ServeOneJabber extends Thread
{
private Socket socket;
private BufferedReader in;
private PrintWriter out;
public ServeOneJabber(Socket s) throws IOException{
socket=s;
in=new BufferedReader(
new InputStreamReader(
socket.getInputStream()));
//Enable auto-flush
out=new PrintWriter(
new BufferedWriter(
new OutputStreamWriter(
socket.getOutputStream())),true);
start();
}
public void run(){
try{
while(true){
String str=in.readLine();
if(str.equals("END"))
break;
System.out.println("Echoing: "+str);
out.println(str);
}
System.out.println("closing...");
}catch(IOException e){
}finally{
try{
socket.close();
}catch(IOException e){}
}
}
}
public class MultiJabberServer
{
static final int PORT=8080;
public static void main(String[] args) throws IOException{
ServerSocket s=new ServerSocket(PORT);
System.out.println("Server Started");
try{
while (true)
{
Socket socket=s.accept();
try{
new ServeOneJabber(socket);
}catch(IOException e){
socket.close();
}
}
}finally{
s.close();
}
}
}
//:JabberClient.java
import java.io.*;
import java.net.*;
public class JabberClient
{
public static void main(String[] args) throws IOException
{
InetAddress addr=InetAddress.getByName(null);
System.out.println("addr = "+addr);
Socket socket=new Socket(addr,JabberServer.PORT);
try{
System.out.println("socket =" +socket);
BufferedReader in=
new BufferedReader(
new InputStreamReader(
socket.getInputStream()));
PrintWriter out =
new PrintWriter(
new BufferedWriter(
new OutputStreamWriter(
socket.getOutputStream())),true);
for(int i=0;i<10;i++){
out.println("howdy "+i);
String str=in.readLine();
System.out.println(str);
}
out.println("END");
}finally{
System.out.println("closing...");
socket.close();
}
}
}