手记

基于TCP的Socket通信传文件

不解释自己看,加了个多线程

package com.immoc;

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.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
    public static void main(String[] args) {
        try {
            ServerSocket serversocket = new ServerSocket(6666);
            System.out.println("**************服务器端已启动***************");
            Socket socket = null;
            int count = 0;
            while(true){
                socket = serversocket.accept();
                ServerThread serverthread = new ServerThread(socket);
                serverthread.start();
                count++;
                InetAddress address = socket.getInetAddress();
                System.out.println("客户端数量:"+count);
                System.out.println("IP:"+address.getHostAddress());
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
package com.immoc;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
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(){
        InputStream is = null;
        BufferedReader br = null;
        OutputStream  os = null;
        PrintWriter pw = null;
        BufferedWriter bw = null;
        try {
            is = socket.getInputStream();
            br = new BufferedReader(new InputStreamReader(is));
            File file = new File("D:\\masm\\weiji1.txt");
                bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)));
                String data = br.readLine();
                while(data!=null){
                    bw.write(data);
                    bw.newLine();
                    data = br.readLine();
                }
                socket.shutdownInput();
                System.out.println("我是客户端:文件已写入!");
            os = socket.getOutputStream ();
            pw = new PrintWriter(os);
            pw.println("欢迎你!");
            pw.flush();
            socket.shutdownOutput();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            try {
            if(br!=null)
                br.close();
            if(bw!=null)
                bw.close();
            if(br!=null)
                br.close();
            if(os!=null)
                os.close();
            if(pw!=null)
                pw.close();
            if(socket!=null)
                socket.close();         
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}
package com.immoc;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
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;
import java.net.UnknownHostException;

public class Client {
    public static void main(String[] args) {
        try {
            Socket socket = new Socket("localhost", 6666);
            String fileNmae = null;
            int fieSize = 0;
            File file = new File("D:\\masm\\weiji.txt");
            if (!file.exists()) {
                System.out.println("客户端:要发送的文件不存在");
            } else {
                BufferedReader br = new BufferedReader(new InputStreamReader(
                        new FileInputStream(file)));
                String line = br.readLine();
                OutputStream os = socket.getOutputStream();
                PrintWriter pw = new PrintWriter(os);
                while (line != null) {
                    pw.println(line);
                    pw.flush();
                    line = br.readLine();
                }
                socket.shutdownOutput();
            }
            InputStream is = socket.getInputStream();
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);
            String data = br.readLine();
            while (data != null) {
                System.out.println("我是服务器端:" + data);
                data = br.readLine();
            }
            socket.shutdownInput();
            br.close();
            isr.close();
            is.close();
            socket.close();
        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}
21人推荐
随时随地看视频
慕课网APP