????д??????
????????????д?????????????????????socket?????е??OutputStream???????д????????????????IO????????
public class ClientSocket {
public static void main(String args[]) {
String host = "127.0.0.1";
int port = 8919;
try {
Socket client = new Socket(host?? port);
Writer writer = new OutputStreamWriter(client.getOutputStream());
writer.write("Hello From Client");
writer.flush();
writer.close();
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
???????IO????
???????????IO?????д?????????????????IO??????й????????????????????
?????????????д
???????????????socket
????int port = 8919;
????ServerSocket server = new ServerSocket(port);
????Socket socket = server.accept();
????????????????????????????socket????????accept???????????????????????socket??accept????????????????????????????????????佨????????????????????
???????????
???????????????socket??????InputStream???????????IO????????????ɡ??????????????????????
public class ServerClient {
public static void main(String[] args) {
int port = 8919;
try {
ServerSocket server = new ServerSocket(port);
Socket socket = server.accept();
Reader reader = new InputStreamReader(socket.getInputStream());
char chars[] = new char[1024];
int len;
StringBuilder builder = new StringBuilder();
while ((len=reader.read(chars)) != -1) {
builder.append(new String(chars?? 0?? len));
}
System.out.println("Receive from client message=: " + builder);
reader.close();
socket.close();
server.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
???????IO????
?????????????????????????????IO?????????????????????
??????????????
???????????????????????????socket??????????????????????????????????????????????????????????????
import java.io.*;
import java.net.*;
public class EchoServer {
public static void main(String args[]) {
// declaration section:
// declare a server socket and a client socket for the server
// declare an input and an output stream
ServerSocket echoServer = null;
String line;
DataInputStream is;
PrintStream os;
Socket clientSocket = null;
// Try to open a server socket on port 9999
// Note that we can't choose a port less than 1023 if we are not
// privileged users (root)
try {
echoServer = new ServerSocket(9999);
}
catch (IOException e) {
System.out.println(e);
}
// Create a socket object from the ServerSocket to listen and accept
// connections.
// Open input and output streams
try {
clientSocket = echoServer.accept();
is = new DataInputStream(clientSocket.getInputStream());
os = new PrintStream(clientSocket.getOutputStream());
// As long as we receive data?? echo that data back to the client.
while (true) {
line = is.readLine();
os.println(line);
}
} catch (IOException e) {
System.out.println(e);
}
}
}
????????????????????????????????????????????????Я??????????????
????15:00 $ curl http://127.0.0.1:9999/?111
????GET /?111 HTTP/1.1
????User-Agent: curl/7.37.1
????Host: 127.0.0.1:9999
????Accept: */*
???????
???????п????-?????????????????????????Java?н???socket?????????????????C??????????д??
????java.net?????????????????????????????????????????????????????У??????????????????API????Sun.*?????????????????????????????????????????????????API?????????????????????????????????????е??????а?????