Java 网络编程
初学Java,练习一个网络编程的实例,有两个问题找不不出来原因(1.客户端发送的消息在服务器端无法显示;2.两个窗口均无法关闭,给窗体加的监听器貌似没起作用),不知如何解决,特来求教。我迷了,希望有前辈或者同行能帮我看看,万分感激。
代码如下:
//客户端程序代码
import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Client extends JFrame implements ActionListener {
Socket sock; //定义套接字对象
JTextArea t1 = new JTextArea();
JTextField t2 = new JTextField(20);
JButton b1 = new JButton("发送");
JButton b2 = new JButton("连接服务器");
DataOutputStream out; //定义数据输出流
DataInputStream in; //定义数据输入流
public Client() {
JScrollPane jsp = new JScrollPane(t1);
t1.setEditable(false);
this.getContentPane().add(jsp,"Center");
JPanel p1 = new JPanel();
p1.add(t2);
p1.add(b1);
JPanel p2 = new JPanel();
p2.add(b2);
this.getContentPane().add(p2,"North");
this.getContentPane().add(p1,"South");
b1.addActionListener(this);
b2.addActionListener(this);
setTitle("客户端");
setSize(340,200);
setVisible(true);
addWindowListener(new WindowAdapter(){
public void WindowClosing(WindowEvent e){
/*try{
out.writeUTF("Bye"); //离开时告诉服务器
}
catch (Exception ee){}*/
dispose(); //释放窗体资源
System.exit(0); //退出窗体
}
});
}
public void actionPerformed(ActionEvent e){
if (e.getSource() == b1){
if (!t2.getText().equals("")){
try{
out.writeUTF(t2.getText()); //向服务器发送消息
t1.append("客户说:"+t2.getText()+"\n");
}catch(Exception ee){}
t2.setText("");
}
}
else {
try {
sock = new Socket("127.0.0.2",3000); //建立与服务器连接的套接字
OutputStream os = sock.getOutputStream(); //根据套接字获得输出流
//使用输出流建立数据输出流(更高级的流)
out = new DataOutputStream(os);
InputStream is = sock.getInputStream(); //根据套接字获得输入流
//根据输入流建立数据输入流(更高级的流)
in = new DataInputStream(is);
Communnion th = new Communnion(this); //建立线程
th.start(); //启动线程
}
catch(IOException ee){
JOptionPane.showMessageDialog(null,"连接服务器失败!");
return;
}
}
}
public static void main(String[] args) {
Client mainFrame = new Client();
}
}
class Communnion extends Thread {
Client fp;
Communnion(Client fp){
this.fp = fp;
}
public void run(){
String msg = null;
while(true){
try {
msg = fp.in.readUTF();
//如果服务器端退出
if (msg.equals("Bye")){
fp.t1.append("服务器已经停止\n");
break;
}
fp.t1.append("服务器说:"+msg+"\n");
}
catch (Exception ee){
break;
}
}
try {
fp.out.close(); //关闭Socket输出流
fp.in.close(); //关闭Socket输入流
fp.sock.close(); //关闭Socket
}
catch (Exception ee){}
}
}
//服务器端代码
import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Server extends JFrame implements ActionListener {
ServerSocket serverSock; //定义服务器套接字
Socket sock; //定义客户套接字
JTextArea t1 = new JTextArea();
JTextField t2 = new JTextField(20);
JButton b1 = new JButton("发送");
DataOutputStream out; //定义数据输出流
DataInputStream in; //定义数据输入流
String cname = null;
public Server(){
try {
//创建一个ServerSocket在端口3000监听客户请求
serverSock = new ServerSocket(3000);
}
catch (IOException e){
JOptionPane.showMessageDialog(null,"服务器连接失败!");
return;
}
JScrollPane jsp = new JScrollPane(t1);
t1.setEditable(false);
this.getContentPane().add(jsp,"Center");
JPanel p1 = new JPanel();
p1.add(t2);
p1.add(b1);
this.getContentPane().add(p1,"South");
b1.addActionListener(this);
setTitle("服务器");
setSize(340,200);
setVisible(true);
try {
//有客户请求到来则产生一个Socket对象,并继续执行
sock = serverSock.accept(); //使用accept()阻塞等待客户请求
//由Socket对象得到输入流,并构造相应的DataOutputStream对象
out = new DataOutputStream(sock.getOutputStream());
//由Socket对象得到输出流,并构造DataInputStream对象
in = new DataInputStream(sock.getInputStream());
out.writeUTF("你已成功连接服务器"); //给客户反馈信息
Communnion th = new Communnion(this); //建立与客户端交互的线程
th.start(); //启动线程
}
catch (Exception e){}
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
try {
out.writeUTF("Bye"); //退出时告诉客户端
}catch (Exception ee){}
dispose(); //释放窗体资源
System.exit(0); //退出窗体
}
});
}
public void actionPerformed(ActionEvent e){
if (!t2.getText().equals("")){
try {
out.writeUTF(t2.getText()); //向客户端发送消息
t1.append("服务器说:"+t2.getText()+"\n");
}catch (Exception ee){}
t2.setText("");
}
}
public static void main(String[] args)
{
Server mainFrame = new Server();
}
}
class Communnion extends Thread {
Server fp;
Communnion(Server fp){
this.fp = fp;
}
public void run(){
String msg = null;
while (true){
try {
msg = fp.in.readUTF(); //等待读取客户端发送的消息
//如果客户退出
if (msg.equals("Bye")){
fp.t1.append("客户端已经离开\n");
break;
}
fp.t1.append("客户说:"+msg+"\n");
}
catch (Exception ee){
break;
}
}
try {
fp.out.close(); //关闭Socker输出流
fp.in.close(); //关闭Socker输入流
fp.sock.close(); //关闭Socket
fp.serverSock.close(); //关闭ServerSocket
}catch (Exception ee){}
}
}
Java 网络编程 socket
[解决办法]
哥们
第一,我修改了你的客户端Connection类名,运行时候客户端信息就可以显示在服务器窗口了
//客户端程序代码
import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Client extends JFrame implements ActionListener {
Socket sock; // 定义套接字对象
JTextArea t1 = new JTextArea();
JTextField t2 = new JTextField(20);
JButton b1 = new JButton("发送");
JButton b2 = new JButton("连接服务器");
DataOutputStream out; // 定义数据输出流
DataInputStream in; // 定义数据输入流
public Client() {
JScrollPane jsp = new JScrollPane(t1);
t1.setEditable(false);
this.getContentPane().add(jsp, "Center");
JPanel p1 = new JPanel();
p1.add(t2);
p1.add(b1);
JPanel p2 = new JPanel();
p2.add(b2);
this.getContentPane().add(p2, "North");
this.getContentPane().add(p1, "South");
b1.addActionListener(this);
b2.addActionListener(this);
setTitle("客户端");
setSize(340, 200);
setVisible(true);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
/*
* try{ out.writeUTF("Bye"); //离开时告诉服务器 } catch (Exception ee){}
*/
dispose(); // 释放窗体资源
System.exit(0); // 退出窗体
}
});
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == b1) {
if (!t2.getText().equals("")) {
try {
out.writeUTF(t2.getText()); // 向服务器发送消息
t1.append("客户说:" + t2.getText() + "\n");
} catch (Exception ee) {
}
t2.setText("");
}
} else {
try {
sock = new Socket("127.0.0.2", 3000); // 建立与服务器连接的套接字
OutputStream os = sock.getOutputStream(); // 根据套接字获得输出流
// 使用输出流建立数据输出流(更高级的流)
out = new DataOutputStream(os);
InputStream is = sock.getInputStream(); // 根据套接字获得输入流
// 根据输入流建立数据输入流(更高级的流)
in = new DataInputStream(is);
ClientCommunnion th = new ClientCommunnion(this); // 建立线程
th.start(); // 启动线程
} catch (IOException ee) {
JOptionPane.showMessageDialog(null, "连接服务器失败!");
return;
}
}
}
public static void main(String[] args) {
Client mainFrame = new Client();
}
}
class ClientCommunnion extends Thread {
Client fp;
ClientCommunnion(Client fp) {
this.fp = fp;
}
public void run() {
String msg = null;
while (true) {
try {
msg = fp.in.readUTF();
// 如果服务器端退出
if (msg.equals("Bye")) {
fp.t1.append("服务器已经停止\n");
break;
}
fp.t1.append("服务器说:" + msg + "\n");
} catch (Exception ee) {
break;
}
}
try {
fp.out.close(); // 关闭Socket输出流
fp.in.close(); // 关闭Socket输入流
fp.sock.close(); // 关闭Socket
} catch (Exception ee) {
}
}
}xception ee) {
}
}
}
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
/*
* try{ out.writeUTF("Bye"); //离开时告诉服务器 } catch (Exception ee){}
*/
dispose(); // 释放窗体资源
System.exit(0); // 退出窗体
}
});