UDP的java实现:
用DatagramSocket建立套接字; 用DatagramPacket 建立UDP数据包。
DatagramPacket pkt = new DatagramPacket(msg.getBytes(),0,msg.getBytes().length,
inetAddress, port);
sck.send(pkt); //send方法发送出去就可以了,不用管连接没连接。
sck.close();
- 接收端:
同样用DatagramSocket建立套接字; 用DatagramPacket 建立UDP数据包。
sct = new DatagramSocket(port);
//繁琐的缓冲数据处理
byte[] buff=new byte[1024];
DatagramPacket pckt = new DatagramPacket(buff, 0, buff.length);
sct.receive(pckt); //对应send 方法,用receive 形成一个阻塞接收。
//以下是需要的信息: pckt.getAddress().getHostName()或getHostAddress();
new String(pckt.getData(), 0, pckt.getLength()); //这个可以得到数据内容。
sct.close();
UDP的特点就是没有谁做服务器的概念,只要定义好相同的端口,谁都可以发送,谁都可以接收。
4. URL的类使用:
统一资源定位符: 可以定位网上的的某一个资源。
协议://ip地址: 端口号/项目名/资源位置。
new URL(地址String);
getProtocol()得到协议名。getHost();得到主机。getPort()得到端口。getPath()得到地址。
getFile()得到文件。getQuery()得到查询名。
利用URL下载文件:
HttpURLConnection urlCon = (HttpURLConnection) url.openConnection(); //返回URL的连接
InputStream strm =urlCon.getInputStream(); //从连接里得到流资源
FileOutputStream fos =new FileOutputStream(str文件流的文件名);
//繁琐的读缓冲区
byte[] buff =new byte[1024];
int len
while((len=inputStream.read(buff))!=1){
fos.write(buffer, 0, len); //往信件的文件流中写入连接流。
}
fos.close();
strm.close();
urlCon.disconnect();
几行代码就可以搞定网络资源的文件了。
Apache Tomcat的启动:
cd /Users/用户名/tomcat/bin,进入到tomcat的bin目录下
输入:./startup.sh + 回车,
如出现错误:“Permission denied”(操作失败,缺少权限)
输入命令,赋予超级管理员权限sudo chmod 755 *.sh + 回车
再次执行命令:./startup.sh + 回车
打开浏览器,输入网址 http://localhost:8080/ ,如果出现一只三角猫,表示tomcat安装成功
关闭的话:./shutdown.sh + 回车
核心配置文件:./conf/server.xml
5. 示例
实现一个标准的TCP/IP通信
下面是服务端,需要先启动,形成一个通信阻塞。核心方法: ServerSocket; Socket; ByteArrayOutputStream; InputStream; buffer的应用以及各种异常处理。
package UseInternet;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class TcpServerDemo01 {
public static void main(String[] args) {
ByteArrayOutputStream baos = null;
ServerSocket serverSocket =null;
Socket socket =null;
InputStream is =null;
try {
serverSocket = new ServerSocket(9999);
socket = serverSocket.accept(); //这里形成一个阻塞,等待接收客户端信息.
is = socket.getInputStream();
baos= new ByteArrayOutputStream();
byte[] buffer2 = new byte[1024];
int len2 = 0;
while ((len2=is.read(buffer2))!=-1){
baos.write(buffer2,0,len2);
}
System.out.println(baos.toString());
System.out.println("Got msg");
} catch (IOException e) {
e.printStackTrace();
} finally {
if (baos!=null){
try {
baos.close();
}catch (IOException e){
e.printStackTrace();
}
}
if (is!=null){
try{
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket!=null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
};
if (serverSocket!=null){
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
下面是客户端代码。
package UseInternet;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
public class TcpClientDemo1 {
public static void main(String[] args) {
Socket socket =null;
OutputStream os =null;
try {
//获得服务器的IP和端口号
InetAddress serverIP= InetAddress.getByName("127.0.0.1");
int port = 9999;
//创建一个socket连接
socket = new Socket(serverIP, port);
os = socket.getOutputStream();
os.write("你好,这是一个网络外发的消息OS".getBytes());
// 关键修复:1.刷新输出流,确保数据全部发送 2.关闭输出流,发送流结束标记
os.flush();
socket.shutdownOutput();
} catch (UnknownHostException e) {
e.printStackTrace();
//throw new RuntimeException(e);
} catch (IOException e) {
e.printStackTrace(); // 建议不要直接throw RuntimeException,方便排查问题
}finally {
if (os!=null){
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(socket!=null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
解析各种网址的IP信息
了解InetAddress.getByName; InetAddress.getLocalHost; getAddress; getCanonicalHostName; getHostAddress; getHostName等常用方法。
import java.net.InetAddress;
import java.net.UnknownHostException;
public class TestInetAddress {
public static void main(String[] args) {
try{
//查询本机地址
InetAddress inetAddress1 = InetAddress.getByName("127.0.0.1");
System.out.println(inetAddress1);
InetAddress inetAddress2 = InetAddress.getByName("localhost");
System.out.println(inetAddress2);
InetAddress inetAddress3 = InetAddress.getLocalHost();
System.out.println(inetAddress3);
InetAddress inetAddress4= InetAddress.getByName("www.baidu.com");
System.out.println(inetAddress4);
InetAddress inetAddress5= InetAddress.getByName("www.Apple.com");
System.out.println(inetAddress5);
System.out.println("====================================");
System.out.println(inetAddress5.getAddress()); //拿IP地址的字节数据(底层二进制,很少用)
System.out.println(inetAddress5.getCanonicalHostName()); //拿规范主机名,强制反向DNS解析
System.out.println(inetAddress5.getHostAddress()); //只拿IP地址字符串(最常用)
System.out.println(inetAddress5.getHostName()); //拿主机名(优先用已知的,不强制反向解析
}catch (UnknownHostException e){
System.out.println(e);
e.printStackTrace();
}
}
}
UDP发送与接收
服务端:需要了解类和方法:DatagramSocket; DatagramPacket; datagramSocket.receive; datagramSocket.send.
package UseInternet;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class TestUDPserver {
public static void main(String[] args) throws Exception {
DatagramSocket sct;
sct = new DatagramSocket(8888);
byte[] buff=new byte[1024];
DatagramPacket dgp= new DatagramPacket(buff,0,buff.length);
sct.receive(dgp); //阻塞接收
System.out.println(dgp.getAddress().getHostName()+"|||"+dgp.getAddress().getHostAddress());
System.out.println(new String(dgp.getData(),0,dgp.getLength()));
sct.close();
}
}更多推荐


所有评论(0)