ServerSocket 简单例题:
package com.kaige123.net;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Test {
public static void main(String[] args) throws Exception {
ServerSocket server = new ServerSocket(8080);
Socket socket = server.accept();
InputStream input = socket.getInputStream();
OutputStream output = socket.getOutputStream();
byte[] b = new byte[1024 * 5];
int len = input.read(b);
String s = new String(b, 0, len);
System.out.println(s);
fin.read(b)
s = "hello 你好客户端!!";
output.write(s.getBytes());
output.close();
input.close();
socket.close();
}
}
|