Android基于TCP协议的Socket通信 实现用户登录 以及服务端的相应

释放双眼,带上耳机,听听看~!

服务端

package com.example;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
/**
 * Created by ChenHe on 2016/10/28.
 * 基于TCP协议的Socket通信,实现用户登录
* 服务端
*/
public class Server {
public static void main(String[] args){
try {
//1.创建一个服务端Socket,即ServerSocket,指定绑定的端口,并且监听端口
ServerSocket serverSocket = new ServerSocket(8888);
//2.调用accept()方法开始监听,等待客户端的连接
System.out.println("***i am server let us begin***");
Socket socket = serverSocket.accept();
//3.获取输入流,并读取客户端信息
InputStream is = socket.getInputStream();//字节输入流
InputStreamReader isr = new InputStreamReader(is);//将字节流转换为字符流
BufferedReader br = new BufferedReader(isr);//为字符流添加缓冲
String info = null;
            while ((info = br.readLine())!=null){//循环读取客户端的信息
System.out.println("The client say:"+info);
}
socket.shutdownInput();//关闭输入流
//4.获取输出流,相应客户端的请求
OutputStream os = socket.getOutputStream();
PrintWriter pw = new PrintWriter(os);//包装为打印流
pw.write("Welcome !!!");
pw.flush();//调用flush()方法将缓冲输出
//5.关闭资源
pw.close();
os.close();
br.close();
isr.close();
is.close();
socket.close();
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

 

服务器打印数据

***i am server let us begin***
The client say锛�UserName: admin;PassWorld锛�123

Process finished with exit code 0

客户端

 

package com.example;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
/**
 * Created by ChenHe on 2016/10/28
 * 客户端.
 */
public class Client {
public static void main(String[] args){
try {
//1.创建客户端Socket,指定服务区地址和端口
Socket socket = new Socket("localhost",8888);
//2.获取输出流,向服务端发送信息
OutputStream os = socket.getOutputStream();//字节输出流
PrintWriter pw = new PrintWriter(os);//将输出流包装为打印流
pw.write("UserName: admin;PassWorld:123");
pw.flush();//刷新
socket.shutdownOutput();//关闭输出流
//3.获取输入流,并读取服务端的相应信息
InputStream is = socket.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String info = null;
            while ((info = br.readLine())!=null){
System.out.println("i am client,server say:"+info);
}
//4.关闭资源
br.close();
is.close();
pw.close();
os.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

 

客户端打印数据

i am client,server say:Welcome !!!

 

 

实现多线程访问 

 

 

package com.example;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
/**
 * Created by ChenHe on 2016/10/28.
 * 服务器线程处理类
*/
public class ServerThread extends Thread {
Socket socket = null;
    public ServerThread(Socket socket){
this.socket = socket;
}
//线程执行的操作,相应客户端的请求
public void run(){
InputStream is = null;//字节输入流
InputStreamReader isr = null;//将字节流转换为字符流
BufferedReader br = null;//为字符流添加缓冲
PrintWriter pw = null;
OutputStream os = null;
        try {
//3.获取输入流,并读取客户端信息
is = socket.getInputStream();
isr = new InputStreamReader(is);
br = new BufferedReader(isr);
String info = null;
            while ((info = br.readLine())!=null){//循环读取客户端的信息
System.out.println("The client say:"+info);
}
socket.shutdownInput();//关闭输入流
//4.获取输出流,相应客户端的请求
os = socket.getOutputStream();
pw = new PrintWriter(os);
pw.write("Welcome !!!");
pw.flush();//调用flush()方法将缓冲输出
} catch (IOException e) {
e.printStackTrace();
}finally {
//5.关闭资源
try {
pw.close();
os.close();
br.close();
isr.close();
is.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

服务端调用方法 

//循环监听客户端的连接
while (true) {
//调用accept()方法开始监听,等待客户端的连接
socket = serverSocket.accept();
//创建一个新的线程
ServerThread serverThread = new ServerThread(socket);
//启动线程
serverThread.start();
count++;
System.out.println("connect count:"+count);
InetAddress address = socket.getInetAddress();
System.out.println("ip:"+address);
}

 

 

人已赞赏
Android文章

Android JAVA 通过URL 获取页面内容

2020-3-27 16:31:51

Android文章

Android ViewPager 的简单应用

2020-3-27 17:46:48

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
有新消息 消息中心
搜索