/**
* 简单聊天 Socket通信 ,起两个程序 一个监听,一个连接。。如果需要多人聊天程序下面那段程序可以用来做服务器监听,实现群聊
*/
//
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import java.io.*;
public class MyTCP extends JFrame implements ActionListener
{
// ------------------------------------------------------------
class MyRecv extends Thread
{
public void run()
{
try
{
while(true)
{
String s = br.readLine();
ta.append(s + "\n");
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
// ------------------------------------------------------------
public void createNetStream() throws Exception
{
br = new BufferedReader(new InputStreamReader(skt.getInputStream()));
pw = new PrintWriter(skt.getOutputStream());
}
public void doServerReady()
{
try
{
ServerSocket server = new ServerSocket(Integer.parseInt(port
.getText()));
skt = server.accept();
createNetStream();
new MyRecv().start();
ta.append("连接成功!" + skt.getPort() + skt.getInetAddress() + "\n");
}
catch (Exception e)
{
ta.append("侦听失败!\n");
}
}
public void doClientLink()
{
try
{
skt = new Socket(ip.getText(), Integer.parseInt(port.getText()));
createNetStream();
new MyRecv().start();
ta.append("连接成功!" + skt.getPort() + skt.getInetAddress() + "\n");
}
catch (Exception e)
{
ta.append("连接失败!\n");
}
}
public void doSend()
{
pw.println(send.getText());
pw.flush();
send.setText("");
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == b1)
{
doServerReady();
}
if(e.getSource() == b2)
{
doClientLink();
}
if(e.getSource() == b3)
{
doSend();
}
}
BufferedReader br; // 读入网络
PrintWriter pw; // 写出到网络
private Socket skt;
private TextArea ta;
private JTextField ip;
private JTextField port;
private JButton b1;
private JButton b2;
private JButton b3;
private JTextField send; // 待发送信息
public MyTCP()
{
this.setBounds(100, 100, 500, 400);
Container cc = this.getContentPane();
// cc.setLayout(new BorderLayout());
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
ta = new TextArea();
cc.add(p1, BorderLayout.NORTH);
cc.add(ta, BorderLayout.CENTER);
cc.add(p2, BorderLayout.SOUTH);
p1.add(new JLabel("ip: "));
ip = new JTextField("127.0.0.1", 10);
p1.add(ip);
p1.add(new JLabel("port: "));
port = new JTextField("8888", 5);
p1.add(port);
b1 = new JButton("侦听");
p1.add(b1);
b2 = new JButton("连接");
p1.add(b2);
p2.setLayout(new BorderLayout());
send = new JTextField(20);
p2.add(send, BorderLayout.CENTER);
b3 = new JButton("发送");
p2.add(b3, BorderLayout.EAST);
// --------------------------------------------------
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
}
public static void main(String[] args)
{
new MyTCP().setVisible(true);
}
}
----------------
多人聊天转发服务器,
/*
keyword:chat,多人聊天,socket,互斥,notifyAll(),wait()
多人聊天服务器,
客户端连接到服务器,由服务器转发.
某个客户端发到服务端后,放在公共变量里,叫醒所有等待的其它客户线程 来发送这里的代码
使用 notifyAll(),wait()
*/
import java.io.*;
import java.net.*;
//服务器接收信息线程
class MyRecv extends Thread
{
private Socket skt;
public MyRecv(Socket skt)
{
this.skt = skt;
}
public void run()
{
try
{
BufferedReader br = new BufferedReader(new InputStreamReader(skt
.getInputStream()));
while(true)
{
String s = br.readLine();
//接收到条信息后,设锁 主要是用来对公共变量MyChat.info来操作锁定
synchronized("kkk")
{
MyChat.info = "[" + skt.getInetAddress() + "] " + s;
//把接受的信息设置好后,
//叫醒所有等待在这个kkk下的线程
"kkk".notifyAll();
}
}
}
catch (Exception e)
{
System.out.println("断线:" + skt.getInetAddress());
}
}
}
//发送线程
class MySend extends Thread
{
private Socket skt;
public MySend(Socket skt)
{
this.skt = skt;
}
public void run()
{
try
{
// 创建网络输出流,输出到网络的另一端
PrintWriter pw = new PrintWriter(skt.getOutputStream());
while(true)
{
synchronized("kkk")
{
//等待kkk,直到被叫醒再向下执行
"kkk".wait();
//当等待被叫醒时也就MyChat.info有新信息要发送了,
pw.println(MyChat.info);
pw.flush();
}
}
}
catch (Exception e)
{
System.out.println("断线:" + skt.getInetAddress());
}
}
}
//主程序,入口,,等待用户连接
public class MyChat
{
public static String info = "hello";
public static void main(String[] args) throws Exception
{
System.out.println("Chat Server Listen at 8888......");
ServerSocket server = new ServerSocket(8888);
while(true)
{
Socket skt = server.accept();
//当前有一个客户端连接过来时对应这个客户端新启动发送和接收信息的线程
System.out.println("连接到:" + skt.getInetAddress() + ","
+ skt.getPort());
new MyRecv(skt).start();
new MySend(skt).start();
}
}
}