package com.anhuixiaozi.socket;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) throws Exception {
try {
ServerSocket server = new ServerSocket(8001);
while(true){
Socket s = server.accept();
Thread thread = new Thread(new ServerThread(s));
thread.start();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
package com.anhuixiaozi.socket;
import java.io.*;
import java.net.*;
public class Client {
public static void main(String[] args) {
try {
Socket s = new Socket("127.0.0.1", 8001);
InputStream ips = s.getInputStream();
OutputStream ops = s.getOutputStream();
// ops.write("request\n\r".getBytes());
PrintWriter pw = new PrintWriter(new OutputStreamWriter(ops),true);
// pw.write("request\n\r");
// pw.flush();
//pw.println("request");
BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in));
System.out.println("please input lowercase:");
pw.println(br1.readLine());
BufferedReader br = new BufferedReader(new InputStreamReader(ips));
System.out.println(br.readLine());
} catch (Exception e) {
e.printStackTrace();
}
}
}
package com.anhuixiaozi.socket;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
public class ServerThread implements Runnable{
Socket s;
ServerThread(Socket s){
this.s = s;
}
public void run() {
try{
boolean bRunning = true;
InputStream ips = null;
OutputStream ops = null;
BufferedReader br = null;
while(bRunning){
ips = s.getInputStream();
ops = s.getOutputStream();
br = new BufferedReader(new InputStreamReader(ips));
// System.out.println(br.readLine());
// ops.write("accept\n\r".getBytes());
PrintWriter pw = new PrintWriter(new OutputStreamWriter(ops),true);
// pw.write("accept\n\r");
// pw.flush();
pw.println(br.readLine().toUpperCase());
}
}catch(Exception e){
}
}
}