java TCP协议将发送的字母转换大写

tcp协议的发送端和接收端的简单交互  接收端将发送端发过来的字母转换成大写发送给发送端。

发送端

package Socket;

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

public class TCPsend {
    public static void main(String[] args) throws IOException {


        Socket socket = new Socket(InetAddress.getLocalHost(),10002);
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        String line = null;
        while ((line = bufferedReader.readLine())!=null){
            if ("over".equals(line))
                break;

            bufferedWriter.write(line);
            bufferedWriter.newLine();
            bufferedWriter.flush();
            BufferedReader bufferedReader1 = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            String a= bufferedReader1.readLine();

            System.out.println(a);

        }
//        socket.shutdownOutput();      在文件上传或下载时  作为结束标记
        bufferedReader.close();
        bufferedWriter.close();
        socket.close();
    }

}



接收端


package Socket;

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

public class TCPrec {
    public static void main(String[] args) throws IOException {

        ServerSocket serverSocket = new ServerSocket(10002);
        Socket ss = serverSocket.accept();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(ss.getInputStream()));
        BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(ss.getOutputStream()));

        String line = null;
        while ((line = bufferedReader.readLine())!=null){
            System.out.println(line);
            bufferedWriter.write(line.toUpperCase()+"\r\n");
            bufferedWriter.flush();
        }
        serverSocket.close();
        ss.close();
    }
}

 

你可能感兴趣的:(java,IO流,TCP协议,java)