62,615
社区成员
发帖
与我相关
我的任务
分享
package com.test.sql;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.util.*;
import java.util.Hashtable;
public class MyServer extends Thread {
// 声名服务器套接字
ServerSocket serverSocket;
public static Hashtable clienttable = null;
public static int cd;
public MyServer() {
try {
serverSocket = new ServerSocket(1001);
} catch (IOException e) {
fail(e, "不能启动服务器");
}
clienttable = new Hashtable(10);
cd = 0;
System.out.println("服务器启动 . . ."); // 服务器初始化成功
start(); // 启动线程
}
public static void fail(Exception e, String str) // 自定义异常
{
System.out.println(str + "." + e);
}
public void run() // 启动线程开始
{
try // 捕获异常
{
while (true) // while里就开始监听客户端数据
{
Socket client = serverSocket.accept(); // 获取客户端Socket 当有数据到达时
// 把数据装到client里
System.out.println("链接到远程的端点地址为为:" + client.getPort());
Connection con = new Connection(client);// 然后 初始化一个Connection
}
} catch (IOException e) // 如果监听有问题就 包报错
{
fail(e, "Not listening");
}
}
public static void main(String args[])// 程序入口
{
// public static Hashtable clienttable =new Hashtable(10);//定义哈希表
new MyServer(); // 初始化 AppServer
}
}
class Connection extends Thread {
// 声明一个客户端的套接字(PS:上面那个是服务器套接字 这个是客户端的)和 一个输入流和输出流
protected Socket netClient; // 客户端的Socket
protected BufferedReader fromClient; // 从客户端获得数据
// protected DataOutputStream out; //发送数据到客户端
protected int port;
public Connection(Socket client) // 这里就是41行 那里初始化Connection
{
netClient = client; // 把client赋值到netClient里
port = netClient.getPort();
MyServer.cd++;
MyServer.clienttable.put(MyServer.cd, netClient);
System.out.println("已经加入哈希表中。");
System.out.println("哈希重组");
System.out.println("哈希表的图:" + MyServer.clienttable.keySet());
System.out.println("哈希表:" + MyServer.clienttable.entrySet());
this.start(); // 启动第二个线程
}
public void run() // 启动第二个线程开始
{
try {
fromClient = new BufferedReader(new InputStreamReader(netClient
.getInputStream()));
//Socket Sw = (Socket) MyServer.clienttable.get(1);
//PrintWriter out = new PrintWriter(Sw.getOutputStream());
while (true) {
String str = fromClient.readLine();
System.out.println(str.trim());
System.out.println(this.getName());
for (Object o : MyServer.clienttable.values()) {
Socket peer = (Socket) o;
PrintWriter out = new PrintWriter(peer.getOutputStream());
out.println(str);
out.flush();
}
}
} catch (Exception e) // 定义IO异常
{
System.out.println("Error in reading object" + e);
}
finally {
try {
netClient.close();
} catch (IOException e) {
}
}
}
}
package com.test.sql;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
public class Client {
/**
* @param args
* @throws IOException
* @throws UnknownHostException
*/
public static void main(String[] args) throws UnknownHostException,
IOException {
Socket socket = new Socket("localhost", 1001);
final InputStream in = socket.getInputStream();
OutputStream out = socket.getOutputStream();
new Thread() {
public void run() {
processIn(in);
}
}.start();
processOut(out);
}
private static void processOut(OutputStream o) {
PrintWriter out = new PrintWriter(o);
Scanner scan = new Scanner(System.in);
while (true) {
String line = scan.nextLine();
out.println(line);
out.flush();
}
}
private static void processIn(InputStream in) {
Scanner scan = new Scanner(in);
while (true) {
System.out.println(scan.nextLine());
}
}
}