Use Socket communication (d)

The socket has a stem, stems primarily a server-side there, and we may encounter the same problem, search online for a long time, here to share solutions. First, the first module to build a class SimpleServer, this class is server, building after a good and run the code on the left button, you can click on run, do not run the module. in this way the server running to accept will block until the connection with the client that he would continue to run down, service port number with Tomcat the port number Terrier here: after running error: BindException: Address already in use: JVM_Bind online search for a long time turned out to be the port is occupied clashed, and then I turn off the sound and the Tomcat, Tomcat how to turn off the very tangled use client ah, online solved: open Tomcat, cmd then type netstat -ano find Tomcat occupied port of PID

I was then 6728 windows windows taskbar at the bottom right to open the Task Manager

Click for more information and then click on the PID in order to find, found 6728, then right to terminate the program, the next time you run the server will not report BindException: Address already in use: JVM_Bind mistake

public  class SimpleServer { 

    public  static  void main (String [] args) throws IOException { 

        // Create a ServerSocket, for connecting the socket to listen for client requests 
        the ServerSocket SS = new new the ServerSocket (9999 );
         // cyclic continue to accept from the client request, the server also generates a corresponding to the Socket 
        the while ( to true ) { 
            the Socket S = ss.accept (); 
            the OutputStream OS = s.getOutputStream (); 
            os.write ( "Hello, you receive a New Year wishes server \ n-! ".getBytes (" UTF-. 8 " )); 
            System.out.println ( " server " ); 
            os.close ();
            s.close();
        }

    }
}

 

Click the top left corner of the green triangle run server

///////////////

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        new Thread(){
            @Override
            public void run() {
                super.run();
                try {
                    Socket socket = new Socket("你的ip地址,cmd输入ipconfig查找", 9999);
                    //设置10秒之后即认为是超时
                    socket.setSoTimeout(10000);
                    BufferedReader br = new BufferedReader(new InputStreamReader(
                            socket.getInputStream()));
                    String line = br.readLine();
                    System.out.println("来自服务器的数据:" + line);

                    br.close();
                    socket.close();

                } catch (UnknownHostException e) {
                    // TODO Auto-generated catch block
                    Log.e("UnknownHost", "来自服务器的数据");
                    e.printStackTrace();
                } catch (IOException e) {
                    Log.e("IOException", "来自服务器的数据");
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }
        }.start();


    }
}

上面是另一个module的主activity,也就是客户端,输出信息代表接收到服务端发送的消息.不要忘了打开网络权限

 

Guess you like

Origin www.cnblogs.com/Ocean123123/p/10960227.html