example: public class Server { private final static int PORT = 10000; ServerSocket server = new ServerSocket(PORT); Socket socket = server.accept; } public class Client { Socket client = new Socket( InetAddress, PORT); BufferedReader ; PrintWriter; }
Socket 实现 /** @version 1.20 2004-08-03 @author Cay Horstmann */import java.io.*; import java.net.*; import java.util.*;/** This program makes a socket connection to the atomic clock in Boulder, Colorado, and prints the time that the server sends. */ public class SocketTest { public static void main(String[] args) { try { Socket s = new Socket("time-A.timefreq.bldrdoc.gov", 13); try { InputStream inStream = s.getInputStream(); Scanner in = new Scanner(inStream);
客户连接server后向服务器要客户列表,里面有ip
然后想和谁说话就去连接这个客户的serverSocket如果想实现穿越代理的功能,可以通过p2p技术
public class Server
{
private final static int PORT = 10000;
ServerSocket server = new ServerSocket(PORT);
Socket socket = server.accept;
}
public class Client
{
Socket client = new Socket( InetAddress, PORT);
BufferedReader ;
PrintWriter;
}
/**
@version 1.20 2004-08-03
@author Cay Horstmann
*/import java.io.*;
import java.net.*;
import java.util.*;/**
This program makes a socket connection to the atomic clock
in Boulder, Colorado, and prints the time that the
server sends.
*/
public class SocketTest
{
public static void main(String[] args)
{
try
{
Socket s = new Socket("time-A.timefreq.bldrdoc.gov", 13);
try
{
InputStream inStream = s.getInputStream();
Scanner in = new Scanner(inStream);
while (in.hasNextLine())
{
String line = in.nextLine();
System.out.println(line);
}
}
finally
{
s.close();
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
}