///////////////////////////////////////////////////////////////////////////////////////////
//第一个 是客户端和服务器端一体的 LANChatV12.java 直接运行就可以
///////////////////////////////////////////////////////////////////////////////////////////
import java.io.*;
import java.net.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
//////////////////////////////////////////////////////////
public class LANChatV12 {
public static void main(String args[]) {
LoginFrame lf = new LoginFrame("输入目标");
lf.show();
}
}
//==== 目标框架
class LoginFrame extends JFrame {
JButton BOK;
JLabel LdesAddr, Lport;
JTextField TFdesAddr, TFport;
String desAddr, port;
LoginFrame() {
}
LoginFrame(String title) {
super(title);
Frame t = this;
BOK = new JButton("确定");
LdesAddr = new JLabel("目标 IP");
Lport = new JLabel("端口 ");
TFdesAddr = new JTextField(desAddr, 12);
TFport = new JTextField(port, 12);
// TFpassword.setEchoChar('*');
BOK.addActionListener(new BOKListener(t));
setBackground(Color.blue);
setBounds(350, 250, 200, 128);
setLayout(new FlowLayout(FlowLayout.CENTER, 5, 7));
add(LdesAddr);
add(TFdesAddr);
add(Lport);
add(TFport);
add(BOK);
setResizable(false);
// setVisible(true);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
class BOKListener implements ActionListener {
Frame t;
BOKListener() {
}
BOKListener(Frame t) {
this.t = t;
}
public void actionPerformed(ActionEvent e) {
desAddr = TFdesAddr.getText();
port = TFport.getText();
t.setVisible(false);
Messenger m = new Messenger(desAddr, port);
m.start();
}
}
}
// ///////////////////////////////////////////////////
class Messenger extends Thread {
String desAddr;
String port;
int iport;
TextArea content, send;
JButton Bsend;
ChatFrame cf;
String title;
Socket client;
ServerSocket ss;
OutputStreamWriter osw = null;
InputStreamReader isr = null;
BufferedReader br;
String line;
boolean flag;// 端口号是否正确
boolean cbc; // can be client ?
int tryTurns = 3; // 客户方式尝试的次数
Messenger() {
}
Messenger(String desAddr, String port) {
super("LANChatMessenger");
content = new TextArea("", 0, 0, TextArea.SCROLLBARS_VERTICAL_ONLY);
send = new TextArea("", 0, 0, TextArea.SCROLLBARS_VERTICAL_ONLY);
Bsend = new JButton("发送");
Bsend.setEnabled(false);
title = "与 " + desAddr + " 聊天";
flag = true;
cbc = true;
cf = new ChatFrame(title, content, send, Bsend);
cf.show();
this.desAddr = desAddr;
this.port = port;
try {
iport = Integer.parseInt(port);
} catch (NumberFormatException nfe) {
content.append("非法的 端口,程序 4 秒后关闭。"n");
flag = false;
}
}
public void run() {
if (!flag) {
try {
Thread.sleep(4000);
} catch (InterruptedException ie) {
System.exit(0);
}
System.exit(0);
}
content.append("正在以客户端方式进行连接....."n");
do {
cbc = true;
try {
client = new Socket(desAddr, iport);
} catch (Exception e) {
content.append("错误,无法连接地址:" + desAddr + ":" + port + '"n');
content.append("等待 1 秒再连接,剩余 [ " + (tryTurns - 1) + " ]次。"n");
cbc = false;
try {
Thread.sleep(1000);
} catch (InterruptedException ie) {
System.exit(0);
}
}
--tryTurns;
} while ((tryTurns > 0) && !cbc);
if (cbc) {
content.append("连接成功,可以开始了。" + ""n"n");
cf.setTitle("与 " + desAddr + ":" + port + " 聊天");
send.requestFocus();
}
else {
content.append("客户端方式失败,现在启动服务器并等待连接。" + '"n');
try {
ss = new ServerSocket(iport);
} catch (IOException ioe) {
content.append(""n无法创建服务,程序将在 4 秒后退出。"n");
try {
Thread.sleep(4000);
} catch (InterruptedException ie) {
System.exit(0);
}
System.exit(0);
}
try {
cf.setTitle("等待连接中....在端口:" + port);
client = ss.accept();
} catch (Exception e) {
content.append(""nss.accept() 方法失败,程序将在 4 秒后退出。"n");
try {
Thread.sleep(4000);
} catch (InterruptedException ie) {
System.exit(0);
}
System.exit(0);
}
content.append("连接成功,可以开始。" + client.getInetAddress().toString()
+ ""n"n");
cf.setTitle("与 " + client.getInetAddress().toString() + ":" + port
+ " 聊天");
send.requestFocus();
}
try {
osw = new OutputStreamWriter(client.getOutputStream());
isr = new InputStreamReader(client.getInputStream());
br = new BufferedReader(isr);
// content.append(""nbr created"n");
} catch (IOException ioe) {
content.append("创建流错误,程序将在 4 秒后退出。"n");
try {
Thread.sleep(4000);
} catch (InterruptedException ie) {
System.exit(0);
}
System.exit(0);
}
Bsend.setEnabled(true);
Bsend.addActionListener(new BsendListener(content, send, osw));
try {
line = br.readLine();
} catch (IOException ioe) {
content.append("流读取错误,程序将在 4 秒后退出。"n");
try {
Thread.sleep(4000);
} catch (InterruptedException ie) {
System.exit(0);
}
System.exit(0);
}
while (true) {
content.append("他说 : " + line + '"n');
try {
Thread.sleep(1000);
} catch (InterruptedException ie) {
System.exit(0);
}
try {
line = br.readLine();
} catch (IOException ioe) {
content.append("流读取错误,程序将在 4 秒后退出。"n");
try {
Thread.sleep(4000);
} catch (InterruptedException ie) {
System.exit(0);
}
System.exit(0);
}
}
}
class BsendListener implements ActionListener {
TextArea content;
TextArea send;
OutputStreamWriter osw;
BsendListener(TextArea content, TextArea send, OutputStreamWriter osw) {
this.content = content;
this.send = send;
this.osw = osw;
}
//将用户输入的信息显示在面板上 同时 发送给对方
public void actionPerformed(ActionEvent e) {
String input;
input = send.getText();
if (input.length() > 0) {
content.append("我说 : " + input + '"n');
try {
osw.write(input + '"n', 0, input.length() + 1);
osw.flush();
} catch (Exception ee) {
content.append("不能发送 """ + input + """ , 发生了错误 : "
+ ee.getMessage() + '"n');
}
send.setText("");
send.requestFocus();
}
}
}
}
// /////////////////////////////////////////////////////////////
//==消息框
class ChatFrame extends JFrame {
TextArea content;
TextArea send;
JButton Bsend;
ChatFrame() {
}
ChatFrame(String frameTitle, TextArea content, TextArea send, JButton Bsend) {
super(frameTitle);
this.content = content;
this.send = send;
this.Bsend = Bsend;
this.send.setEditable(true);
setLayout(null);
setBounds(300, 100, 510, 500);
add(content);
content.setBackground(Color.WHITE);
content.setBounds(2, 0, 500, 340);
content.setFocusable(true);
add(send);
send.setBounds(2, 355, 500, 70);
send.setFocusable(true);
add(Bsend);
Bsend.setBounds(410, 432, 60, 30);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
///////////////////////////////////////////////////////////////////////////////////////////
//下面一个 遇到点问题 就是 一方发出消息 另一方并不能马上收到
///////////////////////////////////////////////////////////////////////////////////////////
package socketTest;
import java.io.*;
import java.net.Socket;
/** 客户端程序 */
public class ClientTest {
public static String line = "";
public static String getLine() {
return line;
}
public static void setLine(String line) {
ClientTest.line = line;
}
public static void main(String args[]) {
try {
// 打开Socket和管道
Socket socket = new Socket("10.5.7.1", 7890);
BufferedReader is = new BufferedReader(new InputStreamReader(socket
.getInputStream()));
PrintWriter os = new PrintWriter(socket.getOutputStream());
// 得到本地输入
BufferedReader ls = new BufferedReader(new InputStreamReader(
System.in));
String readline = ls.readLine();
while (!readline.equals("stop")) {
// 发信息
os.println(readline);
os.flush();
// 收信息
System.err.println(is.readLine());
// 得到本地输入
readline = ls.readLine();
}
// 关闭所有流
os.close();
is.close();
socket.close();
} catch (Exception e) {
System.out.println("Exception :" + e.getMessage());
}
}
}
/////////////////////////////////////////////////////////////////////////////////////////////
package socketTest;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerTest {
public static void main(String[] args) {
ServerSocket server = null;
Socket socket = null;
try {
server = new ServerSocket(7890);
socket= server.accept();
} catch (Exception e) {
System.out.println(e);
}
try {
// 打开Socket和管道
BufferedReader is = new BufferedReader(new InputStreamReader(socket
.getInputStream()));
PrintWriter os = new PrintWriter(socket.getOutputStream());
// 得到本地输入
BufferedReader ls = new BufferedReader(new InputStreamReader(
System.in));
String readline = ls.readLine();
while (!readline.equals("stop")) {
// 发信息
os.println(readline);
os.flush();
// 收信息
System.err.println(is.readLine());
// 得到本地输入
readline = ls.readLine();
}
// 关闭所有流
os.close();
is.close();
socket.close();
server.close();
} catch (Exception e) {
}
}