继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

JAVA TCPsocket通信(传送文件)

长城雇佣军
关注TA
已关注
手记 3
粉丝 4
获赞 10
package com.imooc;

import java.io.Serializable;

public class User implements Serializable
{
private String username;
private String userkey;
public User()
{
    this.userkey = null;
    this.username = null;
}

public User(String username,String userkey)
{
    this.userkey = userkey;
    this.username = username;
}

public String getUsername() {
    return username;
}

public void setUsername(String username) {
    this.username = username;
}

public String getUserkey() {
    return userkey;
}

public void setUserkey(String userkey) {
    this.userkey = userkey;
}
}
package com.imooc;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

public class Client {

public static void main(String[] args) 
{
    try 
    {
        //1. 创建客户端Socket,指定服务器端地址和端口号
        Socket socket = new Socket("127.0.0.1",8888);
        //2. 获取输出流,向服务器端发送信息
        OutputStream os = socket.getOutputStream();  //发出字节输出流
        ObjectOutputStream oos = new ObjectOutputStream(os); //对象字节流
        User user = new User("ladawn","12345678");
        oos.writeObject(user); //序列化
        //3. 关闭socket输出流
        socket.shutdownOutput();
        //4. 获取输入流,接收server的消息
        InputStream is = socket.getInputStream();   //获取得到的字节流
        InputStreamReader isr = new InputStreamReader(is); //字节流转为字符流
        BufferedReader br = new BufferedReader(isr);  //字符流压入缓存区    
        String data = null;
        while( (data=br.readLine()) !=null)
        {
            System.out.println(data);
            data=br.readLine();
        }
        //5.关闭socket输入流
        socket.shutdownInput();
        //6. 关闭socket连接,关闭相关的资源
        socket.close();
        os.close();
        is.close();
        isr.close();
        br.close();
    } 
    catch (UnknownHostException e) 
    {
        e.printStackTrace();
    } 
    catch (IOException e) 
    {
        e.printStackTrace();
    }

}

}
package com.imooc;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;

public class ServerThread extends Thread 
{
Socket socket = null;
public ServerThread(Socket socket)
{
    this.socket = socket;
}

public void run()
{
    ObjectInputStream ois = null;
    OutputStream os = null;
    PrintWriter pw = null;
    try {
        ois = new ObjectInputStream(socket.getInputStream());
        User user = (User)ois.readObject();
        String result = "用户名: "+user.getUsername()+"   密码: "+user.getUserkey();
        System.out.println(result);
        // 关闭socket输入流
        socket.shutdownInput();
        os = socket.getOutputStream();
        pw = new PrintWriter(os);
        //问题:println()和write()只能使用一次吗?第二次发送的数据会无法收到!!!
        pw.println("该用户的用户名和密码为:" + result + "    用户身份核实成功,登录完成");
        //刷新缓存区
        pw.flush();                             
        // 关闭socket输出流
        socket.shutdownOutput();
    } 
    catch (IOException e) 
    {
        e.printStackTrace();
    }
    catch (ClassNotFoundException e) 
    {
        e.printStackTrace();
    }
    finally
    {       
        //关闭socket连接,关闭服务器端,关闭相关资源
        try {
            if(socket != null)
                socket.close();
            if(pw != null)
                pw.close();
            if(os != null)
                os.close();
        } 
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }

}
}

package com.imooc;

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;

public class Server 
{
public static void main(String[] args)
{       
//服务端的多线程通信实现
    try
    {
        // 创建一个“服务器端”Socket,即:ServerSocket,绑定端口建立监听服务。
        ServerSocket serversocket = new ServerSocket(8888);
        System.out.println("-------- 服务器端已启动,开始侦听 --------");
        Socket socket = null;
        while(true)
        {
            // 调用accept()方法监听端口,使Server处于阻塞状态,直到捕捉到client请求,之后建立“一个连接”socket
            socket = serversocket.accept();
            ServerThread sT = new ServerThread(socket);
            sT.run();
        }
    } 
    catch (IOException e)
    {
        e.printStackTrace();
    }
}
}
打开App,阅读手记
2人推荐
发表评论
随时随地看视频慕课网APP