打鱼还是晒网 —— stone

人生不是简单的if和else,很多时候都是在出现问题时才when and then

网络类 UDP TCP Socket


UDP协议:数据报 的发送和接收,

 

/*
 * DatagramSocket 就相当于是一个 应用服务
 * DatagramPacket 作为send()参数时,需要有目标主机的IP和端口号
 * 				     作为receive()参数时,这时操作dp对象的ds需要和send()指定的端口 相同 
 */
public class UDP {
	public static void main(String[] args) throws Exception {
		DatagramSocket ds = new DatagramSocket(56789,InetAddress.getByName("192.168.1.123"));		
		DatagramPacket dp = new DatagramPacket("www.baidu.com".getBytes(),
				"www.baidu.com".getBytes().length,InetAddress.getByName("127.0.0.1"),9999);		
		ds.send(dp);//发送到dp的指定ip和端口
		//System.out.println(dp.getPort());
		
	}
}
class Udpreceive {
	public static void main(String[] args) throws Exception {
		DatagramSocket ds = new DatagramSocket(9999,InetAddress.getByName("192.168.1.123"));
		byte[] buf = new byte[1024];
		DatagramPacket dp = new DatagramPacket(buf, buf.length);
		ds.receive(dp);//接收
		String info = new String(dp.getData(),0,dp.getLength()) + ":" + dp.getAddress().getHostAddress();
		System.out.println(info);
		System.out.println(dp.getSocketAddress());
		/*	getAddress - DatagramPacket			/192.168.1.123
		 * 	getHostAddress - InetAddress		192.168.1.123
		 *       getSocketAddress - DatagramPacket   /192.168.1.123:56789
		 */
	}
}
 

TCP协议:有连接的,点对点传输,

 

/*
 * ServerSocket 服务器端
 * Socket 客户端,需要指定连接的服务器的IP和端口
 * 		socket的输入流    相对于   server的输出流
 * 		socket的输出流    相对于   server的输入流
 */
public class TcpServer implements Runnable {
	Socket s;
	TcpServer(Socket s) {
		this.s = s;
	}
	public static void main(String[] args) throws Exception {
		String port = null;
		File file = new File("src/demo/net/port.txt");

		Properties pro = new Properties();
		pro.load(new FileInputStream(file));
		String temp = pro.getProperty("value");
		if (temp != null) {
			System.out.println("是否启用上次用的端口?(y/n)");
			Scanner input = new Scanner(System.in);
			String str= input.next();
			if (str.equals("y")) {
				port = temp;
			} else {
				System.out.println("请输入 服务器端口号:");
				port = input.next();
				System.out.println(port);
				pro.setProperty("value", port);
				pro.store(new FileOutputStream(file), "this is port");
			}
			
		} else {
			System.out.println("请输入 服务器端口号:");
			Scanner input = new Scanner(System.in);
			port = input.next();
			pro.setProperty("value", port);
			pro.store(new FileOutputStream(file), "this is port");
		
		}
	
		
		ServerSocket server = new ServerSocket(Integer.parseInt(port));
		while (true) {
			Socket soc = server.accept();
			new Thread(new TcpServer(soc)).start();
		}

	}
	@Override
	public void run() {
		// TODO Auto-generated method stub
		InputStream is;
		try {
			is = s.getInputStream();
			OutputStream os = s.getOutputStream();
			BufferedReader br = new BufferedReader(new InputStreamReader(is));
			DataOutputStream dos = new DataOutputStream(os);
			while (true) {
				String start = br.readLine();
				if (start.equalsIgnoreCase("quit")) {
					break;
				}
				String str = new StringBuilder(start).reverse().toString();
				System.out.println(str);
				dos.writeBytes(start + "----->" + str
						+ System.getProperty("line.separator"));
			}
			dos.close();
			br.close();
			s.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
class TcpCLient {
	public static void main(String[] args) throws Exception {

		Socket s = new Socket("127.0.0.1", 18888);
		InputStream is = s.getInputStream();
		OutputStream os = s.getOutputStream();
		DataOutputStream dos = new DataOutputStream(os);
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		BufferedReader brByServer = new BufferedReader(
				new InputStreamReader(is));
		while (true) {
			String word = br.readLine();
			dos.writeBytes(word + System.getProperty("line.separator"));
			if (word.equalsIgnoreCase("quit")) {
				break;
			}
			System.out.println(brByServer.readLine());
		}
		dos.close();
		br.close();
		brByServer.close();
		s.close();
	}
}



阅读更多

扫码向博主提问

去开通我的Chat快问

jjwwmlp456

博客专家

非学,无以致疑;非问,无以广识
版权声明:本文为"匆忙拥挤repeat(stone)"原创文章,转载请注明出处 —— http://blog.csdn.net/jjwwmlp456 https://blog.csdn.net/jjwwmlp456/article/details/6871787
个人分类: Java
想对作者说点什么? 我来说一句

c++ socket类封装(udp/tcp)

2009年11月20日 3KB 下载

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭