Android Socket手机手机通信 求代码

imleihuang 2013-02-23 06:12:10
大家好,最近在研究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();
}
}


谢谢大家。
...全文
101 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
imleihuang 2013-02-24
  • 打赏
  • 举报
回复
有没有实际的例子呢???
引用 3 楼 tf110012 的回复:
服务器收到消息后再发送给另外一台手机,这需要用到消息推送,建议lz去查查,网上有不少消息推送框架的
流星叶雨 2013-02-24
  • 打赏
  • 举报
回复
服务器收到消息后再发送给另外一台手机,这需要用到消息推送,建议lz去查查,网上有不少消息推送框架的

80,362

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧