java socket收发字符串

案例一

server端启动后等待client连接,接收到数据后拼装成字符串并打印
client端连接服务器成功后,向服务器发送字符串

server

package socket;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
    public static int PORT=9527;
    public static void main(String[] args) {
        byte[] buff=new byte[1024];
        try {
            ServerSocket ss=new ServerSocket(PORT);
            System.out.println("启动成功,端口:"+PORT);
            Socket s=ss.accept();
            InputStream is=s.getInputStream();

            int len=is.read(buff);
            System.out.println(new String(buff,0,len));

            s.close();
            ss.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

client

package socket;

import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;

public class Client {
    public static void main(String[] args) {
        try {
            Socket s=new Socket("127.0.0.1",Server.PORT);
            System.out.println("连接成功!");
            OutputStream os=s.getOutputStream();
            os.write("老铁666!一键三连!".getBytes());
            System.out.println("发送成功!");
            s.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

案例二

server端启动后等待client连接,接收到数据后拼装成字符串并打印,然后向客户端发送字符串
client端连接服务器成功后,向服务器发送字符串,然后接收到数据后拼装成字符串并打印

server

package socket;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class Server2 {
    public static int PORT=9527;
    public static void main(String[] args) {
        byte[] buff=new byte[1024];
        try {
            ServerSocket ss=new ServerSocket(PORT);
            System.out.println("启动成功,端口:"+PORT);
            Socket s=ss.accept();
            InputStream is=s.getInputStream();
            OutputStream os=s.getOutputStream();

            int len=is.read(buff);
            System.out.println(new String(buff,0,len));

            os.write("就这?你币没了!".getBytes());

            s.close();
            ss.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

client

package socket;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

public class Client2 {
    public static void main(String[] args) {
        byte[] buff=new byte[1024];
        try {
            Socket s=new Socket("127.0.0.1",Server.PORT);
            System.out.println("连接成功!");
            InputStream is=s.getInputStream();
            OutputStream os=s.getOutputStream();

            os.write("老铁666!一键三连!".getBytes());
            System.out.println("发送成功!");

            is.read(buff);
            System.out.println(new String(buff));
            s.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

原文出处:/show_1EF62QcZwqkL.html