大家好,最近在研究android socket, 现在能够实现手机和pc之间的信息发送,手机发送信息,在pc上显示信息。但是有个问题是,我需要两个手机能够实现,一个手机发送给pc server, pc接受到我的问题后 把这个信息发送给 另一个手机,信息能在另外一个手机上显示出来。我的server pc上的代码是这个:
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();
}
}谢谢大家。
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();
}
}谢谢大家。
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货