1. Server side code:
package test.bwl; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.ServerSocket; import java.net.Socket; import java.util.Calendar; import java.util.Date; import java.util.zip.ZipInputStream; import java.util.zip.ZipOutputStream; import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; public class SocketManager { /** * @param args * @throws IOException */ public static void main(String[] args) { SocketManager manager = new SocketManager(); manager.doListen(); } public void doListen() { ServerSocket server; try { server = new ServerSocket(9991); while (true) { Socket client = server.accept(); new Thread(new SSocket(client)).start(); } } catch (IOException e) { e.printStackTrace(); } } //服务器进程 class SSocket implements Runnable { Socket socket; private final StringBuffer FILE_PATH = new StringBuffer("c:/"); static final int BUFFER = 2048; public SSocket(Socket client) { this.socket = client; } public void run() { Calendar today = Calendar.getInstance(); File file = new File(FILE_PATH.append(today.get(Calendar.YEAR)+"-"+today.get(Calendar.MONTH)+"-"+today.get(Calendar.DAY_OF_MONTH)).toString()); if(!file.exists()){ file.mkdir(); } DataOutputStream serverToClientTextStream = null; FileOutputStream writeFileToLocalBinStream = null; try { serverToClientTextStream = new DataOutputStream(socket.getOutputStream()); serverToClientTextStream.writeUTF("Server received done!"); FileUtils.copyInputStreamToFile(socket.getInputStream(), new File(file.getPath() + "/" + new Date().getTime())); } catch (IOException e) { e.printStackTrace(); }finally { if (socket != null) { try { socket.close(); } catch (IOException e) { e.printStackTrace(); } } } } } }
9991: 服务器端监听端口号。
2. Client side code:
package test.bwl; import io.ReadFile; import java.io.BufferedInputStream; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.InetAddress; import java.net.Socket; import java.net.UnknownHostException; import java.util.zip.ZipFile; import java.util.zip.ZipInputStream; import java.util.zip.ZipOutputStream; import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; public class SocketClient { private static final int BUFFER = 2048; public static void main(String[] args) { Socket socket = null; byte[] buf = new byte[BUFFER]; try { socket = new Socket(InetAddress.getByName("www.domain.com"),9991); // get input and output stream of socket FileOutputStream clientToServerStream = (FileOutputStream)socket.getOutputStream(); DataInputStream serverToClientStream = new DataInputStream(socket.getInputStream()); // write data to server from client InputStream is = new FileInputStream(new File("c:/SYSTEM01.DBF")); BufferedInputStream bis = new BufferedInputStream(is); while(bis.read(buf) != -1){ clientToServerStream.write(buf); clientToServerStream.flush(); } System.out.println(serverToClientStream.readUTF()); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (socket != null) { try { socket.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
www.domain.com: 服务器域名 或者 IP
9991: socket server 监听端口号
3. 配置路由器 NAT设置
3.1 端口触发: 我在电脑上用的是花生壳,3389是花生壳的端口号。
3.2 虚拟服务器
192.168.1.4 : 花生壳主机IP(不过好像写别的也可以)
192.168.1.3 : socket server 主机 局域网IP
4. 启动Socket server side。运行 SocketClient.
经测试成功。