实现功能:私聊和文件传输
客户端:
package my;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.net.Socket;
public class TalkClient {
public TalkClient() {
}
public static void main(String[] args) {
try {
Socket socket = new Socket("127.0.0.1", 4700);
BufferedReader sin = new BufferedReader(new InputStreamReader(System.in));
DataOutputStream os = new DataOutputStream(socket.getOutputStream());
DataInputStream is = new DataInputStream(socket.getInputStream());
System.out.println("样例:1 hello 或者 1 hello.txt");
while(true) {
String[] wj1;
byte[] data;
String[] wj2;
do {
do {
if (sin.ready()) {
String readline = sin.readLine();
os.writeUTF(readline);
os.flush();
String[] talk = readline.split(" ");
wj1 = talk[1].split("\\.");
if (wj1.length <= 1) {
if (talk[1].equals("bye")) {
os.close();
is.close();
socket.close();
System.out.println("您已退出系统!");
return;
}
} else {
File f = new File(talk[1]);
FileInputStream fis = new FileInputStream(f);
long l = f.length();
short m;
int len;
if (l % 512L != 0L) {
m = 512;
len = 512;
} else {
m = 511;
len = 511;
}
data = new byte[m];
while(len >= m) {
len = fis.read(data, 0, m);
os.write(data, 0, len);
os.flush();
}
fis.close();
System.out.println("文件读取发送成功!");
}
}
} while(is.available() <= 0);
String get = is.readUTF();
System.out.println(get);
wj1 = get.split(" ");
wj2 = wj1[2].split("\\.");
} while(wj2.length <= 1);
String name = "E:\\" + wj1[2];
File file = new File(name);
if (!file.exists()) {
file.createNewFile();
System.out.println("文件已新建!");
}
FileOutputStream fos = new FileOutputStream(name);
System.out.println(name);
int n = 512;
int len = 512;
data = new byte[n];
while(len >= n) {
len = is.read(data, 0, n);
fos.write(data, 0, len);
fos.flush();
}
System.out.println("文件已上传成功!");
fos.close();
}
} catch (Exception var15) {
System.out.println("Error" + var15);
}
}
}
服务器端:
package my;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static Socket[] socket = new Socket[10];
static int clientnum = 0;
public Server() {
}
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = null;
boolean listening = true;
try {
serverSocket = new ServerSocket(2222);
} catch (IOException var4) {
System.out.println("Could not listen on port:4700.");
System.exit(-1);
}
while(listening) {
socket[clientnum] = serverSocket.accept();
(new ServerThread(socket[clientnum], clientnum)).start();
++clientnum;
}
serverSocket.close();
}
}
线程:
package my;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.Socket;
public class ServerThread extends Thread {
Socket lssocket = null;
Socket socket = null;
int p;
static int clientnum;
public ServerThread(Socket socket, int num) {
this.socket = socket;
clientnum = num + 1;
this.p = num;
}
public void run() {
try {
DataInputStream is = new DataInputStream(this.socket.getInputStream());
while(true) {
while(true) {
if (is.available() > 0) {
String line = is.readUTF();
String[] talk = line.split(" ");
int number = Integer.parseInt(talk[0]);
this.lssocket = Server.socket[number];
DataOutputStream os = new DataOutputStream(this.lssocket.getOutputStream());
line = "Client " + this.p + ": " + talk[1];
os.writeUTF(line);
os.flush();
String[] wj = talk[1].split("\\.");
if (wj.length > 1) {
System.out.println(this.p + " is send file to " + number + ": " + talk[1]);
int n = 512;
int len = 512;
byte[] data = new byte[n];
while(len >= n) {
len = is.read(data, 0, n);
os.write(data, 0, len);
os.flush();
}
} else {
System.out.println(this.p + " is talk to " + number + ": " + talk[1]);
if (talk[1].equals("bye")) {
System.out.println("客户 " + this.p + " 已退出系统!");
is.close();
this.socket.close();
return;
}
}
}
}
}
} catch (Exception var10) {
System.out.println("Error:" + var10);
}
}
}