展开全部
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
//先运行此文件
public class ServerSockets {
public static void main(String[] args) {
try {
ServerSocket ssServerSocket=new ServerSocket(9999);
Socket socket=ssServerSocket.accept();
InputStream isInputStream=socket.getInputStream();
byte [] bs=new byte[1024];
int length=0;
while((length=isInputStream.read(bs))!=-1)
{
System.out.println(new String(bs));
}
isInputStream.close();
ssServerSocket.close();
} catch (IOException e) {
e.printStackTrace();
}}}
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;
//再运行此文件进行输入传输信息
public class Sockets {
public static void main(String[] args) {
try {
Scanner scanner=new Scanner(System.in);
System.out.println("请输入要传输的信息:");
Socket socket=new Socket("192.168.XXX.XXX", 9999);
OutputStream osOutputStream=socket.getOutputStream();
String info=scanner.nextLine();
osOutputStream.write(info.getBytes());
osOutputStream.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询