练习1(服务器给出反馈)

//发送端
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

public class send {

    public static void main(String[] args) throws IOException {
        Socket s=new Socket("192.168.12.1",10000);//设置主机名和端口号

        OutputStream ops=s.getOutputStream();
        ops.write("hello客户端我来了".getBytes());

        //收到客户端发来的数据
        InputStream is= s.getInputStream();
        byte[]by=new byte[1024];
        int len=is.read(by);
        String data=new String(by,0,len);
        System.out.println("客户端:"+data);
        s.close();
        }
    }
//接受端
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

public class receive {
    public static void main(String[] args) throws IOException {
        ServerSocket ss=new ServerSocket(10000);
//监听信号,一旦发来就接受
        Socket s=ss.accept();
        InputStream is = s.getInputStream();
//字节流接受数据
        byte[]by=new byte[1024];
        int len=is.read(by);
        String st=new String(by,0,len);
        System.out.println("接收到的数据是:"+st);
        //给出反馈
        OutputStream ops=s.getOutputStream();
        ops.write("你好服务端!,我已经接受到了数据".getBytes());

        s.close();

    }
}

练习2(从键盘输入数据进行通信)

import java.io.*;
import java.net.Socket;

//TCP通讯发送端
public class send1 {
    public static void main(String[] args) throws IOException {
        Socket s=new Socket("192.168.12.1",10000);
//字符缓冲流--从键盘输入数据进行传输
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        String line;
        while ((line=br.readLine())!=null){
            //如果输入886则停止通信
            if("886".equals(line)){
                break;
            }
            BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
            bw.write(line);
            bw.newLine();
            bw.flush();

        }
s.close();
    }
}
//接受端
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

public class receive1 {
    public static void main(String[] args) throws IOException {
        ServerSocket ss=new ServerSocket(10000);
        Socket s = ss.accept();
        BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream()));
        String line;
        while ((line=br.readLine())!=null){
            System.out.println(line);
        }
        ss.close();
    }
}

练习3(上传文件,服务端给出反馈)

//发送端
import java.io.*;
import java.net.Socket;
public class send3 {
    public static void main(String[] args) throws IOException {
        Socket s=new Socket("192.168.12.1",10000);
        BufferedReader br=new BufferedReader(new FileReader("E:\\java\\send.txt"));
        BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
        String line;
        while ((line=br.readLine())!=null){
        bw.write(line);
        bw.newLine();
        bw.flush();
    }
        s.shutdownOutput();
        BufferedReader brs=new BufferedReader(new InputStreamReader(s.getInputStream()));
        String fankui=brs.readLine();
        System.out.println(fankui);
        brs.close();
    bw.close();
        s.close();
    }
}
//接收端
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class receive3 {
    public static void main(String[] args) throws IOException {
        ServerSocket ss=new ServerSocket(10000);
        Socket s = ss.accept();
        BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream()));
        BufferedWriter bw=new BufferedWriter(new FileWriter("E:\\java\\receive.txt"));
        String line;
        while ((line=br.readLine())!=null){
            bw.write(line);
            bw.newLine();
            bw.flush();
        }

        //客户端接受到数据给出反馈
        BufferedWriter bws=new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
        bws.write("文件传输成功");
        bws.newLine();
        bws.flush();
        bws.close();
        ss.close();
        bw.close();
    }

}

练习4(多线程实现文件传输)

//发送端
import java.io.*;
import java.net.Socket;

public class send4 {
        public static void main(String[] args) throws IOException {
            Socket s=new Socket("192.168.12.1",10000);
            BufferedReader br=new BufferedReader(new FileReader("E:\\java\\send.txt"));
            BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
            String line;
            while ((line=br.readLine())!=null){
                bw.write(line);
                bw.newLine();
                bw.flush();
            }
            s.shutdownOutput();//自定义结束标记
            BufferedReader brs=new BufferedReader(new InputStreamReader(s.getInputStream()));
            String fankui=brs.readLine();
            System.out.println(fankui);
            brs.close();
            bw.close();
            s.close();
        }


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

//接收端
public class receive4 {
    public static void main(String[] args) throws IOException {
        ServerSocket ss=new ServerSocket(10000);
        Socket s=ss.accept();
        new Thread(new receiveThread(s)).start();

    }
}
import java.io.*;
import java.net.Socket;

public class receiveThread implements Runnable {
    private Socket s;
    public receiveThread(Socket s) {
        this.s = s;
    }

    @Override
    public void run() {
        try {
   BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream()));
//            BufferedWriter bw=new BufferedWriter(new FileWriter("E:\\java\\sercice2.txt"));
//防止文件重名
            int count=0;
File f=new File("E:\\java\\"+count+"sercice2.txt");
while (f.exists()){
    count++;
     f=new File("E:\\java\\"+count+"sercice2.txt");
}
            BufferedWriter bw=new BufferedWriter(new FileWriter(f));
String line;
while ((line=br.readLine())!=null){
    bw.write(line);
    bw.newLine();
    bw.flush();
}
BufferedWriter bwreceive=new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
bwreceive.write("文件传输成功");
bw.newLine();
bw.flush();
bwreceive.close();
        } catch (IOException e) {
            e.printStackTrace();
        }



    }
}