为了账号安全,请及时绑定邮箱和手机立即绑定

JAVA TCPsocket通信(传送文件)

标签:
Java
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();
    }
}
}
点击查看更多内容
2人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消