📄 j_udpclient.java
字号:
// ////////////////////////////////////////////////////////
//
// J_UdpClient.java
//
// Created by Jun-Hai Yong, on XX xx, xxxx (Date)
// ////////////////////////////////////////////////////////
// Readme:
// A simple UDP example: client.
// ////////////////////////////////////////////////////////
// Using this example, please explicitly refer to the book:
// Jun-Hai Yong. Programming in Java.
// Beijing: Tsinghua University Press, 2004.
// 使用本例子,请注明引用:
// 雍俊海. Java 程序设计. 北京: 清华大学出版社, 2004.
// Please note that the author and publisher make no
// warranty of any kind on the examples provided.
// ////////////////////////////////////////////////////////
import java.net.*;
import java.io.*;
public class J_UdpClient
{
public static void main(String args[]) throws IOException
{
DatagramSocket dSocket;
DatagramPacket inPacket;
DatagramPacket outPacket;
InetAddress sAddr;
byte[] inBuffer= new byte[100];
byte[] outBuffer;
String s="Hello";
try
{
dSocket = new DatagramSocket( );
sAddr= InetAddress.getByName("192.168.100.189");
outBuffer= s.getBytes( );
outPacket= new DatagramPacket(outBuffer, outBuffer.length, sAddr, 8000);
dSocket.send(outPacket);
inPacket= new DatagramPacket(inBuffer, inBuffer.length);
dSocket.receive(inPacket);
s= new String (inPacket.getData( ), 0, inPacket.getLength( ));
System.out.println(s);
dSocket.close( );
} // End of try
catch (IOException e)
{
System.out.println("IOException occurred with socket.");
System.out.println (e);
e.printStackTrace( );
} // End of try/structure
} // End of method: main
} // End of class: J_UdpClient
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -