服务器
package netAd;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
ServerSocket server=new ServerSocket(9999);
System.out.println("正在接收");
Socket secket=server.accept();
InputStream is = secket.getInputStream();
//StringBuffer sb = new StringBuffer();
int i=0;
while ((i=is.read())!=-1) {
System.out.print((char)i);
}
is.close();
server.close();
secket.close();
}
}
客户端
package netAd;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
public class Demo {
public static void main(String[] args) throws UnknownHostException, IOException {
Socket socket = new Socket("127.0.0.1",9999);
OutputStream os=socket.getOutputStream();
String info="admin OK";
os.write(info.getBytes());
os.close();
socket.close();
}
}
|