67,538
社区成员
发帖
与我相关
我的任务
分享
public void init() throws Exception {
ServerSocket ss = new ServerSocket(8090);
Socket socket = null;
BufferedReader in;
PrintStream output;
while (true) {
socket = ss.accept();
in = new BufferedReader(new InputStreamReader(socket
.getInputStream()));
String line = in.readLine();
String transno = line.substring(0, 3);
if (transno.equals("007")) {
//***此处省略具体做的事情
OutputStream outputStream = socket.getOutputStream();
output = new PrintStream(outputStream);
output.println("222");
output.flush();
if (output != null)
output.close();
if (in != null)
in.close();
socket.close();
}
}
}
class xxx {
public void init() throws Exception {
ServerSocket ss = new ServerSocket(8090);
Socket socket = null;
BufferedReader in;
PrintStream output;
while (true) {
socket = ss.accept();
MyThread thread = new MyThread(socket);
thread.start();
}
}
}
class MyTHread extends Thread{
private Socket socket;
BufferedReader in;
PrintStream output;
public MyThread(Socket socket){
this.socket = socket;
}
public void run(){
while(true){
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String line = in.readLine();
String transno = line.substring(0, 3);
if (transno.equals("007")) {
//***此处省略具体做的事情
OutputStream outputStream = socket.getOutputStream();
output = new PrintStream(outputStream);
output.println("222");
output.flush();
if (output != null)
output.close();
if (in != null)
in.close();
socket.close();
}
}
}
}
socket = ss.accept();
MyThread thread = new MyThread(socket);
thread.start();
这3句我还是得写在while循环里面吧
也就是
while(true){
socket = ss.accept();
MyThread thread = new MyThread(socket);
thread.start();
}
是这样的吗 ?
socket = ss.accept();
MyThread thread = new MyThread(socket);
thread.start();
--------------------------------------------------
class MyTHread extends Thread{
private Socket socket;
BufferedReader in;
PrintStream output;
public MyThread(Socket socket){
this.socket = socket;
}
public void run(){
while(true){
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String line = in.readLine();
}
}
}