你在客户端没有处理好罢了。
下面是Client端:
import java.net.*;
import java.io.*;
import javax.swing.*;import java.awt.*;
import java.awt.event.*;
/**
* @author administrator
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class SockFrm extends JFrame{
private JTextArea Txtaa = new JTextArea(4,20);
private JTextField Txtfd = new JTextField(20);
private JButton JBon = new JButton("发送");
private String IPstr;
private SockFrm SF;
private Socket soc=null;
private PrintStream out=null;
private BufferedReader in;
private BufferedReader stdIn;
public SockFrm(){
super("网络聊天实例");
Container cont = getContentPane();
cont.setLayout(new FlowLayout());
JLabel lab1 = new JLabel("聊天记录:");
JLabel lab2 = new JLabel("你的发言:");
JMenuBar bar = new JMenuBar();
setJMenuBar(bar);
JMenu FileMenu = new JMenu("文件");
JMenuItem ConSerMenu = new JMenuItem("连接服务器");
ConSerMenu.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent Event){
IPFrame IPF = new IPFrame();
IPF.setParent(SF);
//IPF.setDefaultCloseOperation(IPFrame.EXIT_ON_CLOSE);
}
});
FileMenu.add(ConSerMenu);
bar.add(FileMenu);
cont.add(lab1);
cont.add(Txtaa);
cont.add(lab2);
cont.add(Txtfd);
cont.add(JBon);
setSize(280,260);
setVisible(true);
SF=this;
JBon.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
out.println(Txtfd.getText()+"\n");
out.flush();
}
});
}
public void ConServer(String IP){
String inputLine;
IPstr=IP;
int Num=0;
//JOptionPane.showMessageDialog(null,IPstr,"系统提示",JOptionPane.INFORMATION_MESSAGE);
try{
soc = new Socket(IPstr,9494);
out = new PrintStream(soc.getOutputStream());
in = new BufferedReader(new InputStreamReader(soc.getInputStream()));
//stdIn = new BufferedReader(new InputStreamReader(System.in));
while((Num=in.read())!=0){
char[] cBuf = new char[Num];
in.read(cBuf);
inputLine= new String(cBuf);
Txtaa.setText(Txtaa.getText()+inputLine);
}
}catch (IOException e){
System.out.println(e.getMessage());
}
}
public static void main(String args[]){
SockFrm SF= new SockFrm();
SF.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
下面是Client端:
import java.net.*;
import java.io.*;
import javax.swing.*;import java.awt.*;
import java.awt.event.*;
/**
* @author administrator
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class SockFrm extends JFrame{
private JTextArea Txtaa = new JTextArea(4,20);
private JTextField Txtfd = new JTextField(20);
private JButton JBon = new JButton("发送");
private String IPstr;
private SockFrm SF;
private Socket soc=null;
private PrintStream out=null;
private BufferedReader in;
private BufferedReader stdIn;
public SockFrm(){
super("网络聊天实例");
Container cont = getContentPane();
cont.setLayout(new FlowLayout());
JLabel lab1 = new JLabel("聊天记录:");
JLabel lab2 = new JLabel("你的发言:");
JMenuBar bar = new JMenuBar();
setJMenuBar(bar);
JMenu FileMenu = new JMenu("文件");
JMenuItem ConSerMenu = new JMenuItem("连接服务器");
ConSerMenu.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent Event){
IPFrame IPF = new IPFrame();
IPF.setParent(SF);
//IPF.setDefaultCloseOperation(IPFrame.EXIT_ON_CLOSE);
}
});
FileMenu.add(ConSerMenu);
bar.add(FileMenu);
cont.add(lab1);
cont.add(Txtaa);
cont.add(lab2);
cont.add(Txtfd);
cont.add(JBon);
setSize(280,260);
setVisible(true);
SF=this;
JBon.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
out.println(Txtfd.getText()+"\n");
out.flush();
}
});
}
public void ConServer(String IP){
String inputLine;
IPstr=IP;
int Num=0;
//JOptionPane.showMessageDialog(null,IPstr,"系统提示",JOptionPane.INFORMATION_MESSAGE);
try{
soc = new Socket(IPstr,9494);
out = new PrintStream(soc.getOutputStream());
in = new BufferedReader(new InputStreamReader(soc.getInputStream()));
//stdIn = new BufferedReader(new InputStreamReader(System.in));
while((Num=in.read())!=0){
char[] cBuf = new char[Num];
in.read(cBuf);
inputLine= new String(cBuf);
Txtaa.setText(Txtaa.getText()+inputLine);
}
}catch (IOException e){
System.out.println(e.getMessage());
}
}
public static void main(String args[]){
SockFrm SF= new SockFrm();
SF.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
解决方案 »
- java项目,sqlserver2008,连接数据库的驱动对jdk有要求么?
- java利用poi读取txt文件信息并导入新的excel文件的问题,txt文本无法删除
- 一个ibatis的问题。。。在线等
- 为什么在别处用Eclipse写的程序换台电脑打开工程就出问题了?
- 求助:关于大论文的方向~~
- 关于dom4j与Xpath的烦恼问题。。。。。。。。。。。。。。。。。。。。。。。。。。。
- 请教J2SDK,Apache2,resin2配置环境连接sql2000问题?
- 关于jbuilder设计客户/服务器模式的问题,help!
- 求助:怎么安装uPortal?
- 问题出在哪???请指教
- 高分请求帮忙!关于javamail的,在线等!解决马上结贴
- dom4j 1.5 读入xml文件丢失注释的问题,盼高手解决!
我现在就想分开写
就是一次交互之后写一个,要怎么改啊?
import java.io.*;
import java.net.*;public class Client {
public static void main(String[] args) {
try {
if (args.length != 3) {
System.out.println("need 3 arguments");
System.exit(1);
}
String host = args[0];
String seatno = args[1];
String name=args[2];
Socket socket = new Socket(host,8000);
BufferedReader from_server =
new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter to_server = new PrintWriter(socket.getOutputStream());
to_server.println(seatno);
to_server.println(name);
to_server.flush(); String line;
while ((line = from_server.readLine()) != null) {
System.out.println(line);
}
}
catch (Exception e) {
System.err.println(e);
}
}
}
PrintWriter to_server = new PrintWriter(socket.getOutputStream(),true);