Java基础(14):网络编程之socket
来源:互联网 发布:java模板t 编辑:程序博客网 时间:2024/05/19 05:31
socket有两种协议通信方式:分别为tcp和udp。
Tcp协议通信
tcp协议是一种可靠协议,即使网络暂时阻塞,tcp也能保持通信的可靠性。
服务端:
public class SocketServerTest {public void init(){try {ServerSocket server=new ServerSocket(1000);Socket in=server.accept();InputStream input=in.getInputStream();OutputStream output=in.getOutputStream();PrintWriter out=new PrintWriter(output,true); out.println("hello , client!");System.out.println(input);System.out.println("server start");out.close();output.close();in.close();server.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}
客服端:
public class SocketClient {public void init(){try {Socket s=new Socket("127.0.0.1",1000);InputStream in=s.getInputStream();BufferedReader br=new BufferedReader(new InputStreamReader(in));String line=br.readLine();System.out.println(line);br.close();s.close();} catch (UnknownHostException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}
Udp协议通信
udp协议相对tcp协议是一种不可靠协议,每次传送的数据大小也有限制。
服务端:
public class UdpServer {private int SIZE=4096;private int PORT=1111;private byte []buf=new byte[SIZE];private DatagramPacket packet,out;public void init() throws IOException{try {packet=new DatagramPacket(buf, buf.length);DatagramSocket socket=new DatagramSocket(PORT);socket.receive(packet);System.out.println(new String(buf,0,packet.getLength()));byte[] data=("server receive").getBytes();out=new DatagramPacket(data,data.length,packet.getSocketAddress());socket.send(out);} catch (SocketException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}
客户端
public class UdpClient {private int SIZE=4096;private String IP="127.0.0.1";private int PORT=1111;private byte []buf=new byte[SIZE];private DatagramPacket packet,out;public void init() throws IOException{try {DatagramSocket socket=new DatagramSocket();byte[] outBuf=("client say hello").getBytes();packet=new DatagramPacket(buf, buf.length,InetAddress.getByName(IP),PORT);out=new DatagramPacket(outBuf, outBuf.length,InetAddress.getByName(IP),PORT);socket.send(out);socket.receive(packet);System.out.println(new String(buf,0,packet.getLength()));} catch (SocketException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}
0 0
- Java基础(14):网络编程之socket
- Java Socket网络编程基础
- java 网络编程 基础socket
- java网络socket编程(一)之基础简介
- c#网络编程之------------Socket编程基础
- 网络编程之Socket编程基础
- Java网络编程之Socket
- JAVA网络编程之Socket
- java网络编程之SOCKET
- java网络编程之Socket
- Java基础之Socket编程
- Unix网络编程 之 socket基础
- java网络编程基础Socket通信应用
- java网络编程之socket编程笔记
- Java网络编程之Socket编程
- Java网络编程之Socket编程
- Socket网络编程基础
- socket网络编程基础
- mysql数据库常用命令
- 【黑马程序员】StringBuffer,Array,Integer,BigInteger,Date,Calendar,集合——Java复习笔记
- [sicily]1433. Optimal Parking
- @Controller、@RequestMapping
- 在IOS8及IOS9中使用UILocationNotification本地推送时不显示推送
- Java基础(14):网络编程之socket
- UIPickerView
- Android开源BezierView:仿QQ未读消息99+条的红色气泡
- 数据结构与算法javascript描述(五) 选择排序
- AudioManager的简单使用
- UIDatePicker
- Http基础学习
- Photos框架学习备忘
- [Leetcode]Ugly Number