Socket一般形式

import java.net.*;//
import java.io.*;
public class SocketTest{
public static void main(String [] args)throws Exception{
Socket socket1 =null;
InputStream is =null;
OutputStream os =null;
String serverIP = "58.251.37.197";
int port =10158;
String data="hello";
socket1=new Socket(serverIP,port);
os=socket1.getOutputStream();
os.write(data.getBytes());
//接收数据
is = socket1.getInputStream();
byte[] b = new byte[1024];
int n = is.read(b);
//输出反馈数据
System.out.println("服务器反馈:" + new String(b,0,n));
}
}

猜你喜欢

转载自blog.csdn.net/u011381797/article/details/80303713