Java网络编程总结
1.TCPServer.javaimport java.net.*;import java.io.*;public class TCPServer {public static void main(String[] args) throws Exception {ServerSocket ss = new ServerSocket(6666);while(true) {Socket s = ss.accept();System.out.println("a client connect!");DataInputStream dis = new DataInputStream(s.getInputStream());System.out.println(dis.readUTF());dis.close();s.close();}}}
2.TCPClient.java
import java.net.*;import java.io.*;public class TCPClient {public static void main(String[] args) throws Exception {Socket s = new Socket("127.0.0.1", 6666);OutputStream os = s.getOutputStream();DataOutputStream dos = new DataOutputStream(os);//Thread.sleep(30000);dos.writeUTF("hello server!");dos.flush();dos.close();s.close();}}
3.客户端、服务器端单向通信
TestServer.java
/*范例名称:简单的client/server程序 * 源文件名称:TestClient.java/TestServer.java *要点: *1. Java Socket编程步骤 *2. Socket/ServerSocket类用法 *3. 通过Socket对象可以获取通信对方Socket的信息 */import java.net.*;import java.io.*;public class TestServer {public static void main(String args[]) {try {ServerSocket s = new ServerSocket(8888);while (true) {Socket s1 = s.accept();OutputStream os = s1.getOutputStream();DataOutputStream dos = new DataOutputStream(os);dos.writeUTF("Hello," + s1.getInetAddress() + "port#" +s1.getPort() + "bye-bye!");dos.close();s1.close();}}catch (IOException e) {e.printStackTrace();System.out.println("程序运行出错:" + e);}}}
TestClient.java
/*范例名称:简单的client/server程序 * 源文件名称:TestClient.java/TestServer.java *要点: *1. Java Socket编程步骤 *2. Socket/ServerSocket类用法 *3. 通过Socket对象可以获取通信对方Socket的信息 */import java.net.*;import java.io.*;public class TestClient {public static void main(String args[]) {try {Socket s1 = new Socket("127.0.0.1", 8888);InputStream is = s1.getInputStream();DataInputStream dis = new DataInputStream(is);System.out.println(dis.readUTF());dis.close();s1.close();} catch (ConnectException connExc) {connExc.printStackTrace();System.err.println("服务器连接失败!");} catch (IOException e) {e.printStackTrace();}}}
4.客户端服务器端双向通信
TestSockServer.java
import java.io.*; import java.net.*;public class TestSockServer {public static void main(String[] args) { InputStream in = null; OutputStream out = null; try { ServerSocket ss = new ServerSocket(5888); Socket socket = ss.accept(); in = socket.getInputStream(); out = socket.getOutputStream(); DataOutputStream dos = new DataOutputStream(out); DataInputStream dis = new DataInputStream(in); String s = null; if((s=dis.readUTF())!=null) { System.out.println(s); System.out.println("from: "+socket.getInetAddress()); System.out.println("Port: "+socket.getPort()); } dos.writeUTF("hi,hello"); dis.close(); dos.close(); socket.close(); } catch (IOException e) {e.printStackTrace();}}}
TestSockClient.java
import java.net.*;import java.io.*;public class TestSockClient {public static void main(String[] args) { InputStream is = null; OutputStream os = null; try { Socket socket = new Socket("localhost",5888); is = socket.getInputStream(); os = socket.getOutputStream(); DataInputStream dis = new DataInputStream(is); DataOutputStream dos = new DataOutputStream(os); dos.writeUTF("hey"); String s = null; if((s=dis.readUTF())!=null); System.out.println(s); dos.close(); dis.close(); socket.close(); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) {e.printStackTrace();}}}
页:
[1]