80,492
社区成员
发帖
与我相关
我的任务
分享
public class TCPServerA {
public static void main(String[] args) throws IOException {
InetAddress AddressOne = InetAddress.getLocalHost();//返归本地主机的IP地址
System.out.print("本地主机名和IP是:");
System.out.println(AddressOne);
ServerSocket ss = new ServerSocket(8879);
Socket s = ss.accept();
OutputStream os = s.getOutputStream();
PrintWriter pw = new PrintWriter(os);
pw.print("now time = " + new Date());
pw.flush();
pw.close();
s.close();
ss.close();
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.d("socket","1");
try {
Log.d("socket","2");
Socket s = new Socket("192.168.1.209",8879);
Log.d("socket","3");
InputStream is = s.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String str = br.readLine();
System.out.println(str);
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}