给你一段代码,不知道能不能解决你的问题
可以直接传字符串,也可以将字符串数组封装对象进行传输。
服务端代码:
import java.io.ObjectInputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class MyServer {
public static void main(String[] args) throws Exception{
ServerSocket ss = new ServerSocket(9091);
Socket s = ss.accept();
System.out.println("new connection");
ObjectInputStream oi = new ObjectInputStream(s.getInputStream());
String [] arr = (String[]) oi.readObject();
System.out.println(arr[0] + arr[1]);
}
}
客户端代码:
import java.io.ObjectOutputStream;
import java.net.Socket;
public class Mysocket {
public static void main(String[] args) throws Exception {
Socket s = new Socket("localhost",9091);
ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream());
out.writeObject(new String[]{"123","232"});
out.flush();
out.close();
}
} |