import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; public class MyJavaServer { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub System.out.print("MyJavaServer Start Runing/n"); try { ServerSocket serverSocket=new ServerSocket(54321); while(true) { System.out.println("接收用户连接:"); //接受客户端请求 Socket client=serverSocket.accept(); System.out.println("accept:"); try { //接受客户端信息 BufferedReader in=new BufferedReader(new InputStreamReader(client.getInputStream())); String str=in.readLine(); System.out.println("read:"+str); //向服务器发送消息 PrintWriter out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(client.getOutputStream())),true); out.println("Server Message:"+str); in.close(); out.close(); }catch(Exception ex) { System.out.println(ex.getMessage()); ex.printStackTrace(); } finally { client.close(); System.out.println("close"); } } } catch (IOException e) { System.out.println(e.getMessage()); } } }
Android下使用Socket连接网络电脑(2)
内容版权声明:除非注明,否则皆为本站原创文章。