Server端:
1. ServerSocket s = new ServerSocket(PORT);//初始化一个ServerSocket对象
2. Socket socket = s.accept();//accept()函数阻塞等待一个socket连接,有连接进来(就是client调用Socket(addr,port)的时候)会返回一个socket对象。
在accetp返回的时候,会返回一个当前连接对应的socket对象。
3. 通过socket.getInputStream()等,获取输入或者输出流进行操作
Client端:
1. InetAddress addr = InetAddress.getByName(xx) 等获取服务器地址
2. Socket socket= new Socket(addr,PORT);//根据地址和port,初始化一个socket对象。
如果服务器没有Socket.accept等待连接,则返回一个异常(Connection refused: connect)
单用户
Server端
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.*;
import java.io.*;
import javax.net.ServerSocketFactory;
public class java_socket_server {
public static final int port =8080;
public static void main(String[] args) {
try {
//可以直接用ServerSocket也可以用ServerSocketFactory
ServerSocket s= null; //new ServerSocket(port);
ServerSocketFactory sf = ServerSocketFactory.getDefault();
s = sf.createServerSocket(port);
System.out.println("Server start to accept");
Socket socket = s.accept();
System.out.print("connection accepted:" + socket);
System.out.println(" ");
Process list;
BufferedReader in = new BufferedReader(
new InputStreamReader(
socket.getInputStream())
);
PrintWriter out = new PrintWriter(
new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream())
)
,true);
while(true){
String str = in.readLine();
if(str.equals("END")) {
System.out.println("END received");
break;
}
System.out.println("Echoing:" + str);
out.println(str+" fromeServer");
}
System.out.println("closing....");
socket.close();
s.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Client端
import java.io.IOException;
import java.net.*;
import java.io.*;
import java.util.*;
public class java_socket_client {
public static void main(String[] args){
InetAddress addr;
try {
addr = InetAddress.getByName(null);
System.out.println("addr "+addr);
Socket socket1;
Socket socket2;
try {
socket1 = new Socket(addr,8080);
System.out.println("socket 1 = " + socket1);
BufferedReader in1 = new BufferedReader(
new InputStreamReader(socket1.getInputStream())
);
PrintWriter out1 = new PrintWriter(
new BufferedWriter(
new OutputStreamWriter(socket1.getOutputStream()))
,true);
for(int i=0;i<10;i++) {
out1.println("howdy" + i);
String str=in1.readLine();
System.out.println(str);
}
out1.println("END");
socket1.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Server端的log:
Server start to accept
connection accepted:Socket[addr=/127.0.0.1,port=64048,localport=8080]
Echoing:howdy0
Echoing:howdy1
Echoing:howdy2
Echoing:howdy3
Echoing:howdy4
Echoing:howdy5
Echoing:howdy6
Echoing:howdy7
Echoing:howdy8
Echoing:howdy9
END received
closing....
Client端log
addr localhost/127.0.0.1
socket 1 = Socket[addr=localhost/127.0.0.1,port=8080,localport=64048]
socket 2 = Socket[addr=localhost/127.0.0.1,port=8080,localport=64049]
howdy0 fromeServer
howdy1 fromeServer
howdy2 fromeServer
howdy3 fromeServer
howdy4 fromeServer
howdy5 fromeServer
howdy6 fromeServer
howdy7 fromeServer
howdy8 fromeServer
howdy9 fromeServer
多用户
Server端:
每有一个连接过来就创建一个进行通信
//: MultiJabberServer.java
// A server that uses multithreading to handle
// any number of clients.
import java.io.*;
import java.net.*;
class ServeOneJabber extends Thread {
private Socket socket;
private BufferedReader in;
private PrintWriter out;
public ServeOneJabber(Socket s) throws IOException {
socket = s;
in =
new BufferedReader(
new InputStreamReader(
socket.getInputStream()));
// Enable auto -flush:
out =
new PrintWriter(
new BufferedWriter(
new OutputStreamWriter(
socket.getOutputStream())), true);
// If any of the above calls throw an
// exception, the caller is responsible for
// closing the socket. Otherwise the thread
// will close it.
start(); // Calls run()
}
public void run() {
try {
while (true) {
String str = in.readLine();
if (str.equals("END")) break;
System.out.println("Echoing: " + str);
out.println(str);
}
System.out.println("closing...");
} catch (IOException e) {
} finally {
try {
socket.close();
} catch(IOException e) {}
}
}
}
public class MultiJabberServer {
static final int PORT = 8080;
public static void main(String[] args) throws IOException {
ServerSocket s = new ServerSocket(PORT);
System.out.println("Server Started");
try {
while(true) {
// Blocks until a connection occurs:
Socket socket = s.accept();
try {
new ServeOneJabber(socket);
} catch(IOException e) {
// If it fails, close the socket,
// otherwise the thread will close it:
socket.close();
}
}
} finally {
s.close();
}
}
} ///:~