标签:blog io ar os 使用 sp java 数据 on
package andy.network.test; import java.io.IOException; import java.io.PrintStream; import java.net.ServerSocket; import java.net.Socket; /** * @author Zhang,Tianyou * version:2014-11-25 上午10:49:11 * * */ public class FirstTCPServer { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { //创建服务器 ServerSocket server = null; Socket socket = null; PrintStream out = null; //服务器在9999上等待客户端的访问 server = new ServerSocket(9999); System.out.println("服务器端已经运行,等待客户的响应。。。"); //程序阻塞 等待客户端的访问 socket = server.accept(); //向客户端输出信息 String str = "hello andy."; out = new PrintStream(socket.getOutputStream()); out.print(str); out.close(); socket.close(); server.close(); } }
package andy.network.test; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.Socket; import java.net.UnknownHostException; /** * @author Zhang,Tianyou * version:2014-11-25 上午11:02:17 * * */ public class FirstTCPSocket { /** * @param args * @throws IOException * @throws UnknownHostException */ public static void main(String[] args) throws UnknownHostException, IOException { // 定义客户端 Socket client = null; // 设置地址和端口 client = new Socket("localhost", 9999); BufferedReader buf = null; // 结束服务端流 buf = new BufferedReader(new InputStreamReader(client.getInputStream())); String str = buf.readLine(); System.out.println("服务器端通知信息:" + str); buf.close(); client.close(); } }
package andy.network.test; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; /** * @author Zhang,Tianyou * version:2014-11-25 上午11:41:22 * * 聊天服务器端 */ public class ChatServer { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { ServerSocket server = null; Socket client = null; // 监听9999端口的连接 server = new ServerSocket(9999); boolean flag = true; while (flag) { System.out.println("服务器运行,等待客户端连接。"); client = server.accept(); // 对每一个客户端请求启动一个线程 new Thread(new ChatThread(client)).start(); } server.close(); } }
package andy.network.test; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintStream; import java.net.Socket; /** * @author Zhang,Tianyou * version:2014-11-25 上午11:24:26 * * 使用多线程机制,在每个用户响应之后启动一个新任务 * */ public class ChatThread implements Runnable { // 接受客户端 private Socket client = null; public ChatThread(Socket client) { this.client = client; } @Override public void run() { // 向客户端发送数据 PrintStream out = null; // 接收客户端发来的数据 BufferedReader buff = null; try { // 得到客户端的输入信息 buff = new BufferedReader(new InputStreamReader( client.getInputStream())); out = new PrintStream(client.getOutputStream()); //标志是否一个客户端的操作完毕 boolean flag = true; while (flag) { //不断的接收信息 String str = buff.readLine(); if(str == null || "".equals(str)){ flag = false;//客户端操作结束 }else{ if("bye".equals(str)){ flag = false; out.println("bye-bye"); }else { //向客户端回显输入 out.println("you input:" + str); } } } out.close(); client.close(); } catch (Exception e) { System.out.println("服务器异常!"); } } }
package andy.network.test; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintStream; import java.net.Socket; import java.net.UnknownHostException; /** * @author Zhang,Tianyou * version:2014-11-25 上午11:53:03 * * 聊天客户端程序 * */ public class ChatClient { public static void main(String[] args) throws UnknownHostException, IOException { Socket client = null; client = new Socket("localhost", 9999); // 接收服务器端的信息 BufferedReader buff = null; // 向服务器端发送数据 PrintStream out = null; BufferedReader input = null; // 获取键盘输入数据 input = new BufferedReader(new InputStreamReader(System.in)); out = new PrintStream(client.getOutputStream()); buff = new BufferedReader( new InputStreamReader(client.getInputStream())); // 标志位 boolean flag = true; while (flag) { System.out.println("输入信息:"); String str = input.readLine(); // 向服务器端输出信息 out.println(str); if ("bye".equals(str)) { flag = false; } else { String chatCont = buff.readLine(); System.out.println(chatCont); } } client.close(); buff.close(); } }
package andy.network.test; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; /** * @author Zhang,Tianyou * version:2014-11-25 下午3:04:10 * * UDP客户端 */ public class UDPClient { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { // 客户在9999端口监听 DatagramSocket dSocket = null; dSocket = new DatagramSocket(9999); // 定义接收数据的字节长度 byte[] buff = new byte[1024]; DatagramPacket dPacket = null; // 指定接收数据的长度1024 dPacket = new DatagramPacket(buff, 1024); System.out.println("等待接受数据。。"); //接收数据 dSocket.receive(dPacket); String str = new String(dPacket.getData(), 0, dPacket.getLength()) + "from" + dPacket.getAddress().getHostAddress() + ":" + dPacket.getPort(); System.out.println(str); //关闭数据报套接字 dSocket.close(); } }
package andy.network.test; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; /** * @author Zhang,Tianyou * version:2014-11-25 下午3:13:38 * * UDP服务器端 * */ public class UDPServer { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { DatagramSocket dSocket = null; //服务端在3333端口监听 dSocket = new DatagramSocket(3333); DatagramPacket dPacket = null; //要发生的数据 String str = "hello andy!"; //向目标端口地址发送数据报 dPacket = new DatagramPacket(str.getBytes(), str.length(), InetAddress.getByName("localhost") , 9999); System.out.println("发送数据报"); dSocket.send(dPacket); dSocket.close(); } }
标签:blog io ar os 使用 sp java 数据 on
原文地址:http://blog.csdn.net/fengshizty/article/details/41448261