80,362
社区成员
发帖
与我相关
我的任务
分享
public class AndroidServer extends ServerSocket {
private static final int SERVER_PORT = 54321;
public AndroidServer() throws IOException {
super(SERVER_PORT);
try {
while (true) {
Socket socket = accept();
new CreateServerThread(socket);
}
} catch (IOException e) {} finally {
close();
}
}
//--- CreateServerThread
class CreateServerThread extends Thread {
private Socket client,clientG;
private BufferedReader in;
private PrintWriter out, outG, outS;
public CreateServerThread(Socket s) throws IOException {
client = s;
clientG = s;
in = new BufferedReader(new InputStreamReader(client.getInputStream(), "GB2312"));
in = new BufferedReader(new InputStreamReader(clientG.getInputStream(), "GB2312"));
// System.out.println("" + )
// System.out.println("in ist : " + in);
// out = new PrintWriter(client.getOutputStream(), true);
//out.println("--- Welcome ---");
start();
}
public void run() {
try {
String line = in.readLine().toString();
String message = line;
System.out.println(message.length() );
String device = message.substring(0,6).toLowerCase();
System.out.println(device);
message = message.substring(8);
System.out.println(message );
System.out.println(line);
System.out.println(!(line.equals("bye")) + " line 97");
boolean compare = !(line.equals("bye"));
//System.or!(line.equals("bye")
while (compare) {
if(device.equals("NEXUSG".toLowerCase())){
outG = new PrintWriter(client.getOutputStream(), true);
outG.println(message);
}
if(device.equals("NEXUSS".toLowerCase())){
outS = new PrintWriter(clientG.getOutputStream(), true);
outS.println(message);
}
}
if(device.equals("NEXUSS")){
outS.close();
}
if(device.equals("NEXUSG")){
outG.close();
}
in.close();
//out.println("--- See you, bye! ---");
client.close();
} catch (IOException e) {
e.printStackTrace();
} catch(NullPointerException e){
e.printStackTrace();
}
}
private String createMessage(String line) {
return "Server says: " + line;
}
}
public static void main(String[] args) throws IOException {
new AndroidServer();
}
}