7网络与通信-




3.android中基于socket的网络通信:

  使用serversocket建立聊天服务器-1:

MyServerSocker.java:

package com.jikexueyuan.testmyserverscoket.main;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.Socket;

public class ChatSocket extends Thread {
    
    Socket socket;
    
    public ChatSocket(Socket s) {
        this.socket = s;
    }
    
    public void out(String out) {
        try {
            socket.getOutputStream().write(out.getBytes("UTF-8"));
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    
    @Override
    public void run() {
        int count = 0;
        while(true) {
            count++;
            out("loop+"+count);
            try {
                sleep(500);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}

ServerListener.java:

package com.jikexueyuan.testmyserverscoket.main;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

import javax.swing.JOptionPane;

public class ServerListener extends Thread {

    @Override
    public void run() {
        try {
            ServerSocket serverSocket = new ServerSocket(12346);
            while(true) {
                //block
                Socket socket = serverSocket.accept();
                //建立连接
                JOptionPane.showMessageDialog(null,"有客户端链接到了本机的123245端口");
                //将socket传递给新的线程
                new ChatSocket(socket).start();
            }
            
            
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

ChatSocket.java:

package com.jikexueyuan.testmyserverscoket.main;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.Socket;

public class ChatSocket extends Thread {
    
    Socket socket;
    
    public ChatSocket(Socket s) {
        this.socket = s;
    }
    
    public void out(String out) {
        try {
            socket.getOutputStream().write(out.getBytes("UTF-8"));
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    
    @Override
    public void run() {
        int count = 0;
        while(true) {
            count++;
            out("loop+"+count);
            try {
                sleep(500);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}

使用serversocket建立聊天服务器-2:

 

7网络与通信-

上一篇:伸缩栏


下一篇:leetcode0-global的用法