2 qq 28728033 qq_28728033 于 2015.06.04 12:05 提问

急求Java socket编程实现两个客户端通信通过服务器转发不要图形界面不用连接数据库

实现两个客户端通信通过服务器转发不要图形界面不用连接数据库 求源码

6个回答

sinat_27985725
sinat_27985725   2015.06.04 12:13

可以找本java的书照着代码敲敲看

91program
91program   Ds   Rxr 2015.06.04 12:44

Android Socket 编程(WIFI 和 ADB)
这里提供了服务器端的源代码,客户端可在网上查找一下。

pop32399
pop32399   2015.06.04 12:53

这个问题问得好,不太好回答

renwotao2009
renwotao2009   2015.06.04 12:55

网上马士兵的javase自己下载

muier
muier   2015.06.04 13:37

网上马士兵的javase自己下载

wojiushiwo945you
wojiushiwo945you   Ds   Rxr 2015.12.30 20:13

这个简单,实例代码,服务器端:

 import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class ChatTTCPServer {
    public ChatTTCPServer(int port, String name) throws IOException {
        ServerSocket server = new ServerSocket(port);
        Socket client = server.accept();
        if(client!=null){
            new ChatTTCPSocketJFrame(name, client);
            System.out.println("收到消息。。。");
            server.close();
        }
    }

    public static void main(String[] args) throws IOException {
        new ChatTTCPServer(9999, "Jack");
    }

}

客户端:

 import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JToolBar;

public class ChatTTCPSocketJFrame extends JFrame implements ActionListener {
    /**
    * 
    */
    private static final long serialVersionUID = 1L;
    private String name;
    private JTextArea text_receiver;
    private JTextField text_sender;
    private PrintWriter cout;
    private JButton buttons[];

    public ChatTTCPSocketJFrame(String name, Socket socket) throws HeadlessException, IOException {
        super("聊天室" + name + "" + InetAddress.getLocalHost() + ":" + socket.getPort());
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.text_receiver = new JTextArea();
        this.text_receiver.setEditable(false);
        this.getContentPane().add(new JScrollPane(this.text_receiver));
        JToolBar toolBar = new JToolBar();
        this.getContentPane().add(toolBar, "South");
        toolBar.add(this.text_sender = new JTextField(30));
        String[] buttonstr = { "发送", "离线" };
        buttons = new JButton[buttonstr.length];
        for (int i = 0; i < buttonstr.length; i++) {
            toolBar.add(buttons[i] = new JButton(buttonstr[i]));
            buttons[i].addActionListener(this);
        }
        this.setSize(400, 400);
        this.setVisible(true);
        this.name = name;
        this.cout = new PrintWriter(socket.getOutputStream(), true);
        this.cout.println(name);
        BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        String line = "连接" + br.readLine() + "成功";
        while (line != null && !line.endsWith("bye"))
        {
            text_receiver.append(line + "\r\n");
            line = br.readLine();
        }
        br.close();
        this.cout.close();

        //发送完数据
        socket.close();
        buttons[0].setEnabled(false);
        buttons[1].setEnabled(false);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("发送"))
        {
            this.cout.println(name + "说" + text_sender.getText());
            text_receiver.append("我说: " + text_sender.getText() + "\n");
            text_sender.setText("");
        }
        if (e.getActionCommand().equals("离线")) {
            text_receiver.append("离线");
            this.cout.println(name + "离线\n" + "bye\n");
            buttons[0].setEnabled(false);
            buttons[1].setEnabled(false);
        }
    }

    public static void main(String[] args) throws IOException {
        new ChatTTCPSocketJFrame("Mike", "localhost", 9999);
    }

    public ChatTTCPSocketJFrame(String name, String host, int port) throws IOException {
        this(name, new Socket(host, port));
    }

}

Csdn user default icon
上传中...
上传图片
插入图片