java socket网络编程
2016-10-11 15:59
218 查看
tcp网络编程
1 客户端向服务端传输数据//客户端 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("你好!"); dos.flush(); dos.close(); s.close(); } }
//服务端 public class TCPServer { public static void main(String[] args) throws Exception { ServerSocket ss = new ServerSocket(6666); //监听6666端口 while(true) { Socket s = ss.accept(); //侦听并接受到此套接字的连接 DataInputStream dis = new DataInputStream(s.getInputStream()); System.out.println(dis.readUTF()); //你好! dis.close(); s.close(); } } }
2 服务端向客户端传输数据
//客户端 public class TestClient { public static void main(String[] args) throws Exception { try { Socket s = new Socket("127.0.0.1", 8888); //指定主机的指定端口 InputStream is = s.getInputStream(); //拿到输出流 DataInputStream dis = new DataInputStream(is); System.out.println(dis.readUTF()); //Hello /127.0.0.1 port 56637 bye bye! dis.close(); s.close(); }catch (ConnectException connExc) { connExc.printStackTrace(); System.err.println("服务器连接失败"); }catch (IOException e) { e.printStackTrace(); } } }
//服务端 public class TestServer { public static void main(String[] args) throws Exception { try { ServerSocket ss = new ServerSocket(8888); //监听6666端口 while(true) { Socket s = ss.accept(); //侦听并接受到此套接字的连接 DataOutputStream dos = new DataOutputStream(s.getOutputStream()); //得到客户端的IP和端口号 dos.writeUTF("Hello " + s.getInetAddress() + " port " + s.getPort()+" bye bye!"); dos.flush(); dos.close(); s.close(); } }catch (IOException e) { e.printStackTrace(); System.out.println("程序运行出错"+e); } } }
3 双向传输数据
//客户端 public class TestSocketClient { public static void main(String[] args) throws Exception { InputStream in = null; OutputStream out = null; try { Socket socket = new Socket("127.0.0.1", 5888); in = socket.getInputStream(); out = socket.getOutputStream(); DataInputStream dis = new DataInputStream(in); DataOutputStream dos = new DataOutputStream(out); dos.writeUTF("hello, server!"); String s = null; if((s = dis.readUTF()) != null) { System.out.println(s); //hello,client! } dos.close(); dis.close(); socket.close(); }catch(UnknownHostException e) { e.printStackTrace(); }catch (IOException e) { e.printStackTrace(); } } }
//服务端 public class TestSocketServer { public static void main(String[] args) throws Exception { InputStream in = null; OutputStream out = null; try { ServerSocket ss = new ServerSocket(5888); Socket socket = ss.accept(); in = socket.getInputStream(); out = socket.getOutputStream(); DataInputStream dis = new DataInputStream(in); DataOutputStream dos = new DataOutputStream(out); String s = null; if((s = dis.readUTF()) != null) { System.out.println(s); //hello, server! System.out.println("from " + sock 4000 et.getInetAddress()); //from /127.0.0.1 System.out.println("port " + socket.getPort()); //port 56759 } dos.writeUTF("hello,client!"); dis.close(); dos.close(); socket.close(); }catch(IOException e) { e.printStackTrace(); } } }
4 模拟聊天demo
//客户端 public class ChatClient { public static void main(String[] args) { InputStream in = null; OutputStream out = null; InputStreamReader isr= null; PrintWriter pw = null; BufferedReader br = null; BufferedWriter bw = null; Socket client; try { client = new Socket("127.0.0.1", 9999); in = client.getInputStream(); out = client.getOutputStream(); isr = new InputStreamReader(in); pw = new PrintWriter(out); br = new BufferedReader(isr); BufferedReader sin = new BufferedReader(new InputStreamReader(System.in)); String line; line = sin.readLine(); while(!line.equals("goodbye")) { pw.println(line); pw.flush(); System.out.println("client send:" + line); System.out.println("client receive:" + br.readLine()); line = sin.readLine(); } pw.close(); br.close(); client.close(); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
//服务端 public class ChatServer { public static void main(String[] args) { InputStream in = null; OutputStream out = null; InputStreamReader isr = null; PrintWriter pw = null; BufferedReader br = null; try { ServerSocket serverSocket = new ServerSocket(9999); Socket server = serverSocket.accept(); String line; in = server.getInputStream(); out = server.getOutputStream(); isr = new InputStreamReader(in); pw = new PrintWriter(out); br = new BufferedReader(isr); BufferedReader sin = new BufferedReader(new InputStreamReader( System.in)); System.out.println("server receive:" + br.readLine()); line = sin.readLine(); while (!line.equals("goodbye")) { pw.println(line); pw.flush(); System.out.println("server send:" + line); System.out.println("server receive:" + br.readLine()); line = sin.readLine(); } pw.close(); br.close(); server.close(); serverSocket.close(); } catch (IOException e) { e.printStackTrace(); } } }
5 tcp协议特点
建立连接
可靠
效率低
udp网络编程
1 客户端向服务端传数据//udp客户端 public class TestUDPClient { public static void main(String[] args) throws Exception { long n = 10000L; ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(baos); dos.writeLong(n); byte[] buf = baos.toByteArray(); //byte[] buf = new String("hello").getBytes(); DatagramPacket dp = new DatagramPacket(buf, buf.length, new InetSocketAddress("127.0.0.1", 5678)); DatagramSocket ds = new DatagramSocket(9999); ds.send(dp); ds.close(); } }
//udp服务器端 public class TestUDPServer { public static void main(String[] args) throws Exception { byte[] buf = new byte[1024]; //接收客户端数据 DatagramPacket dp = new DatagramPacket(buf, buf.length); //数据包 DatagramSocket ds = new DatagramSocket(5678); //监听udp的5678端口,tcp还有一个5678端口 while(true) { ds.receive(dp); ByteArrayInputStream bais = new ByteArrayInputStream(buf); DataInputStream dis = new DataInputStream(bais); System.out.println(dis.readLong()); //10000 } } }
2 udp协议特点
无连接
不可靠
效率高
相关文章推荐
- Java Socket网络编程七
- Java Socket网络编程
- 循序渐进Java Socket网络编程(多客户端、信息共享、文件传输)
- java socket网络编程(多线程技术)
- Java Socket网络编程初级入门
- Java Socket网络编程常见异常(转)
- Java socket网络编程TCP客户端的实现
- Java Socket网络编程初级入门(转载)
- Java Socket网络编程常见异常
- java socket网络编程
- 9.java学习之渐进Java Socket网络编程(多客户端、信息共享、文件传输)(转自开源中国)
- 循序渐进Java Socket网络编程(多客户端、信息共享、文件传输)
- Java Socket网络编程的经典例子(转)
- Java Socket网络编程的经典例子
- Java Socket网络编程的经典例子
- Java Socket网络编程
- 循序渐进Java Socket网络编程(多客户端、信息共享、文件传输)
- Java Socket网络编程初级入门
- 对使用Java Socket网络编程的详细讲解
- 循序渐进Java Socket网络编程(多客户端、信息共享、文件传输)