服务端
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class SocketManager {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) {
SocketManager manager = new SocketManager();
manager.doListen();
}
public void doListen() {
ServerSocket server;
try {
server = new ServerSocket(9991);
while (true) {
Socket client = server.accept();
new Thread(new SSocket(client)).start();
System.out.println("**************************");
}
} catch (IOException e) {
e.printStackTrace();
}
}
// 服务器进程
class SSocket implements Runnable {
Socket client;
public SSocket(Socket client) {
this.client = client;
}
public void run() {
BufferedInputStream input;
PrintWriter output;
try {
while(true){
System.out.println(client.isClosed());
System.out.println(client.isConnected());
input = new BufferedInputStream(client.getInputStream());
output = new PrintWriter(client.getOutputStream(),true);
//
System.out.println("等待接收数据:");
String listMsg = readInputStream(input);//input.readUTF();
if(listMsg!=null){
output.println("Recive: " + listMsg + " \r\n Thx...");
System.out.println("Recive: " + listMsg);
}else{
client.close();
break;
}
//listMsg = readInputStream(input);
// output.println("Recive Second: " + listMsg
// + " \r\n Thx...");
// System.out.println("Recive Second: " + listMsg);
}
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("结束");
}
private String readInputStream(BufferedInputStream in)
throws IOException {
String data = "";
int s = in.read();
if (s == -1) {
return null;
}
data += "" + (char) s;
int len = in.available();
if (len > 0) {
byte[] byteData = new byte[len];
in.read(byteData);
data += new String(byteData);
}
return data;
}
}
}
客户端
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
public class SocketClient {
public static void main(String[] args) {
Socket socket = null;
try {
socket = new Socket("127.0.0.1", 9991);
//向服务器端第一次发送字符串
OutputStream netOut = socket.getOutputStream();
PrintWriter doc = new PrintWriter(netOut,true);
BufferedInputStream in = new BufferedInputStream(socket.getInputStream());
//向服务器端第二次发送字符串
doc.println("list");
doc.flush();
String res = readInputStream(in);
System.out.println(res);
doc.println("bye");
res = readInputStream(in);
System.out.println(res);
doc.close();
in.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
}
}
}
}
private static String readInputStream(BufferedInputStream in) throws IOException {
String data = "";
int s = in.read();
if (s == -1) {
return null;
}
data += ""+(char)s;
int len = in.available();
if(len > 0) {
byte[] byteData = new byte[len];
in.read(byteData);
data += new String(byteData);
}
return data;
}
}
socket
最新推荐文章于 2022-05-12 17:57:55 发布