TCP/UDP编程

package TestConnetify;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;

//TCP编程例一:客户端给服务端发送信息。服务端输出此信息到控制台上
//网络编程实际上是Socket的编程
//先开服务端,后开客户端,不然会出错
import org.junit.Test;

public class TestTCP1 {
	@Test
	// 客户端
	public void client() {
		Socket socket = null;
		OutputStream os = null;
		try {
			// 1.创建一个Socket的对象,通过构造器指明发送给服务端的IP地址,以及其接收程序的端口号
			socket = new Socket(InetAddress.getByName("127.0.0.1"), 5327);// host主机/端口
			os = socket.getOutputStream();// 2.getOutputStream()发送数据方法返回其的对象
			os.write("我是客户端,请多关照".getBytes());// 3.具体的输出过程
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			// 4.关闭相应的流和Socket对象
			if (os != null) {
				try {
					os.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (socket != null) {
				try {
					socket.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}

	// 服务端
	@Test
	public void server() {
		ServerSocket ss = null;
		Socket s = null;
		InputStream is = null;
		try {
			// 1.创建一个ServerSocket的对象,通过构造器指明自身的端口号
			ss = new ServerSocket(5327);
			// 2.调用其accept()方法,返回一个Socket的对象
			s = ss.accept();
			// 3.调用Socket对象的getInputStream获取一个从客户端发送过来的输入流
			is = s.getInputStream();
			// 4.对获取的输入流进行的操作
			byte[] b = new byte[20];
			int len;
			while ((len = is.read(b)) != -1) {
				String str = new String(b, 0, len);
				System.out.print(str);
			}
			System.out.println("收到来自于" + s.getInetAddress().getHostAddress()
					+ "的链接");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			// 5.关闭相应的流以及Socket、SercerSocket的对象
			if (is != null) {
				try {
					is.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (s != null) {
				try {
					s.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (ss != null) {
				try {
					ss.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
}
<pre name="code" class="java">package TestConnetify;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

import org.junit.Test;
/*
 * TCP编程例二:客户端给服务端发送信息,服务端将信息打印到控制台上,同时发送”已收到信息“给客户端
 */
public class TestTCP2 {
	//客户端
	@Test
	public void client(){
		Socket socket=null;
		OutputStream os=null;
		InputStream is=null;
		try {
			socket = new Socket(InetAddress.getByName("127.0.0.1"),5337);
			os = socket.getOutputStream();
			os.write("我是客户端".getBytes());//客户端已发送
			//shutdownOutput():执行此方法,显示告诉服务端发送完毕
			socket.shutdownOutput();
			is = socket.getInputStream();
			byte[] b=new byte[20];
			int len;
			while((len=is.read(b))!=-1){
				String str=new String(b,0,len);
				System.out.print(str);
			}
		}catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{	
			if(is!=null){
				try {
					is.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(os!=null){
				try {
					os.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(socket!=null){
				try {
					socket.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
	//服务端
	@Test
	public void server(){
		ServerSocket ss=null;
		Socket s=null;
		InputStream is=null;
		OutputStream os=null;
		try {
			ss = new ServerSocket(5337);
			s = ss.accept();
			is = s.getInputStream();
			byte[] b=new byte[20];
			int len;
			while((len=is.read(b))!=-1){
				String str=new String(b,0,len);
				System.out.print(str);
			}
			os = s.getOutputStream();
			os.write("我已收到你的信息".getBytes());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if(ss!=null){
				try {
					ss.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}				
			}
			if(s!=null){
				try {
					s.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}				
			}
			if(is!=null){
				try {
					is.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}				
			}
			if(os!=null){
				try {
					os.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}				
			}
		}
	}
}


 

package TestConnetify;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

import org.junit.Test;

//TCP 编程例三:从客户端发送文件给服务端,服务端保存到本地,并返回“发送成功”给客户端并关闭链接
public class TestTCP3 {
	@Test
	public void client(){
		Socket socket=null;
		OutputStream os=null;
		FileInputStream fis=null;
		try {
			socket=new Socket(InetAddress.getByName("127.0.0.1"),5334);  //1.创建socket对象
			os=socket.getOutputStream();//2,从本地获取一个文件发送给服务端
			fis=new FileInputStream(new File("test.txt"));
			byte [] b=new byte[1024];
			int len;
			while((len=fis.read(b))!=-1){
				os.write(b,0,len);
			}//3.关闭相应的流和socket对象
			InputStream is=socket.getInputStream();
			byte[] b1=new byte[1024];
			int len1;
			while((len1=is.read(b1))!=-1){
				String str=new String(b1,0,len1);
				System.out.print(str);
			}
		}  catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{	
			if(socket!=null){
				try {
					socket.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(os!=null){
				try {
					os.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(fis!=null){
				try {
					fis.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
	@Test
	public void server(){
		ServerSocket ss=null;
		Socket s=null;
		InputStream is=null;
		FileOutputStream fos=null;
		try {
			ss=new ServerSocket(5334);//创建一个ServerSOcket对象
			s=ss.accept();//调用方法 返回一个Scoket对象
			is=s.getInputStream();//将从客户端发送来的信息保存到本地
			fos=new FileOutputStream(new File("test1.txt"));
			byte[] b=new byte[1024];
			int len;
			while((len=is.read(b))!=-1){
				fos.write(b,0,len);
			}
			//发送消息的信息反馈给客户端
			System.out.println("收到来自于"+s.getInetAddress().getHostAddress()+"的消息");
			OutputStream os=s.getOutputStream();
			os.write("图片已发送成功".getBytes());
		} catch (IOException e) {//关闭Socket及ServerSocket的对象
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if(ss!=null){
				try {
					ss.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}				
			}
			if(s!=null){
				try {
					s.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}				
			}
			if(is!=null){
				try {
					is.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}				
			}
			if(fos!=null){
				try {
					fos.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}				
			}
		}
	}
}
package TestConnetify;
/*
 * testUDP
 */
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;

import org.junit.Test;

public class TestUDP {
	@Test
	public void send(){
		DatagramSocket ds=null;
		try {
			ds = new DatagramSocket();
			byte[]b="你好,我是要发送的数据".getBytes();
			DatagramPacket pack=new DatagramPacket(b, 0, b.length,InetAddress.getByName("127.0.0.1"),5237);
			ds.send(pack);
		}catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if(ds!=null){
				ds.close();
			}
		}
	}
	@Test
	public void receive(){
		DatagramSocket ds=null;
		try {
			ds = new DatagramSocket(5237);
			byte[] b=new byte[1024];
			DatagramPacket pack=new DatagramPacket(b,0,b.length);
			ds.receive(pack);
			String str=new String(pack.getData(), 0, pack.getLength());
			System.out.println(str);
		}  catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if(ds!=null){
				ds.close();
			}
		}
	}
}


package TestConnetify;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

//URL:同意资源定位符,一个URL的对象,对应着互联网上一个资源,通过URL的对象调用其相应的方法,将次资源读取”下载“
public class TestURL {
	public static void main(String[] args) throws Exception {
		URL url=new URL("http://127.0.0.1:8080/examples/HelloWorld.txt"); //资源的路径 
		/*
		 * public String getProtocol()    获取该URL的协议名
		 * public String getHost() 		获取该URL的主机名
		 * public String getPort() 		获取端口号
		 * public String getPath() 		获取文件路径
		 * public String getFile() 			获取文件名
		 * public String getRef()  			获取文件的相对位置
		 * public String getQuery() 			获取查询名
		 */
		System.out.println(url.getProtocol());
		System.out.println(url.getHost());
		
		InputStream is=url.openStream();
		byte[] b=new byte[20];
		int len;
		while((len=is.read(b))!=-1){
			String str=new String(b,0,len);
			System.out.print(str);
		}
		is.close();
		//如果既有数据的输入 又有数据的输出 则考虑用URLConnettion
		URLConnection urlConn=url.openConnection();
		InputStream is1=urlConn.getInputStream();
		FileOutputStream fos=new FileOutputStream(new File("hello1.txt"));
		byte[] b1=new byte[20];
		int len1;
		while((len1=is.read(b1))!=-1){
			fos.write(b1,0,len1);
		}
		fos.close();
		is1.close();
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值