---------------------- android培训、java培训、期待与您交流! ----------------------
Java网络编程是由Socket类来实现,网络传输有UDP和TCP协议,前者不需要服务器端,通过数据包封装传输,数据传输最多64k,后者直接建立Socket流连接,可传输很大数据
需求:Java建立两个程序,用UDP协议实现发送端发送消息,接收端能接收到。
import java.net.*; import java.io.*; public class UdpSend{ public void getHost(String ipAddress) { try{ java.net.InetAddress ia = InetAddress.getByName(ipAddress); String ip = ia.getHostAddress(); String name= ia.getHostName(); System.out.println(ip); System.out.println(name); } catch(Exception e) { e.printStackTrace(); } } public void udpSendData() throws Exception { DatagramSocket ds = new DatagramSocket(8087); //发送的内容编码为字节数组,数据原为键盘录入 BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String line = null; while((line=br.readLine())!=null) { if("886".equals(line)) break; byte [] by = line.getBytes(); //发送的地址 InetAddress ia = InetAddress.getByName("192.168.0.149"); //将发送数据封装成包的形式 DatagramPacket dp = new DatagramPacket(by,0,by.length,ia,1212); ds.send(dp); } br.close(); ds.close(); } public static void main(String [] args) throws Exception { UdpSend ur = new UdpSend(); ur.udpSendData(); } } class UdpReceive{ public void udpReceiveData() throws Exception { DatagramSocket ds = new DatagramSocket(1212); while(true) { //接收的内容 byte[] by = new byte[1024]; //将发送数据封装成包的形式 DatagramPacket dp = new DatagramPacket(by,by.length); ds.receive(dp);//堵塞式接收 //接收到的数据解码字符串 String data = new String(dp.getData(),0,dp.getLength()); System.out.println(dp.getAddress().getHostAddress()+"::"+data+ "::"+dp.getPort()); } } public static void main(String [] args) throws Exception { UdpReceive ur = new UdpReceive(); ur.udpReceiveData(); } }需求2:用UDP协议实现聊天程序
基本思路:使用一个main函数,建立两个线程,分别是发送消息和接收消息,同时运行两个线程
需求3:用TCP协议实现服务器端和客户端的通讯,需要保证当客户端连接上时,服务器端知道客户端的ip地址,并且服务器端能收到客户端发过来的信息。
如果有多个客户端连接服务器时,需要使用多线程,才能保证服务器端数据安全。
import java.util.*; import java.net.*; import java.io.*; public class ThreadDemo { /** *源:Socket读取流 *目的:Socket输出流 **/ public void serverTcp() throws Exception{ ServerSocket ss = new ServerSocket(8001); while(true) { Socket s = ss.accept();//阻塞式等待连接 //获取连接客户端的ip地址 new Thread(new ServerThread(s)).start(); } } public static void main(String[] args) throws Exception { ThreadDemo td = new ThreadDemo(); td.serverTcp(); } } class ClientTcp{ public void ClientTcp() throws Exception{ Socket s = new Socket(); InetAddress ia = InetAddress.getByName("192.168.0.149"); s.connect(new InetSocketAddress(ia,8001)); while(true) { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); OutputStream os = s.getOutputStream(); PrintWriter pw = new PrintWriter(os,true);//自动刷新 String line = br.readLine(); pw.println(line); if("over".equals(line)) break; } } public static void main(String[] args) throws Exception { ClientTcp td = new ClientTcp(); td.ClientTcp(); } } class ServerThread implements Runnable{ Socket s; ServerThread(Socket s){ this.s=s; } public void run () { try{ String ip = s.getInetAddress().getHostAddress(); System.out.println(ip+" 客户端连接上......"); while(true){ InputStream is = s.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is)); String line = br.readLine(); if("over".equals(line)) break; System.out.println(line); } System.out.println(ip+" 客户端断开......"); } catch(Exception e) { new RuntimeException("接受失败"); } } }
需求4:用TcP协议通过多线程的方式将文件上传到服务器上
import java.util.*; import java.net.*; import java.io.*; public class TcpDemo { /** *源:Socket读取流 *目的:Socket输出流 **/ public static void main(String[] args) throws Exception { ServerSocket ss = new ServerSocket(8001); while(true) { Socket s = ss.accept(); new Thread(new MyThread(s)).start(); } } } class ClientTcp{ public void sendFile(String[] args) throws Exception { Socket s = new Socket("192.168.0.149",8001); File f = new File(args[0]); InputStream is = new FileInputStream(f); OutputStream os = s.getOutputStream(); int len = 0; byte[] by = new byte[1024]; while((len=is.read(by))!=-1) { os.write(by,0,len); } s.shutdownOutput(); byte[] by1 = new byte[1024]; int arr = s.getInputStream().read(by1); System.out.println(new String(by1,0,arr)); s.close(); } public static void main(String[] args) throws Exception { ClientTcp td = new ClientTcp(); td.sendFile(args); } } class MyThread implements Runnable { Socket s; MyThread(Socket s) {this.s=s;} public void run() { try{ //获取连接客户端的ip地址 String ip = s.getInetAddress().getHostAddress(); System.out.println(ip+" 客户端连接上......"); InputStream is = s.getInputStream(); int count = 1; File f1 = new File("d:\\"+ip+".jpg"); while(f1.exists()) f1 = new File("d:\\"+ip+"_("+(count++)+").jpg"); OutputStream os = new FileOutputStream(f1); byte[] by = new byte[1024]; int len = 0; while((len=is.read(by))!=-1) { os.write(by,0,len); } PrintWriter out = new PrintWriter(s.getOutputStream(),true); out.println("上传成功"); } catch(Exception e){} } }
---------------------- android培训、java培训、期待与您交流! ----------------------
详细请查看:http://edu.csdn.net/heima