Java 网络编程

来源:互联网 发布:土行孙翻墙软件怎么样 编辑:程序博客网 时间:2024/06/03 17:03

--------------------- ASP.Net+Unity开发、.Net培训、期待与您交流! ----------------------

Java 网络编程



网络通信的条件:
1、找到对方IP
2、数据要发送到对方指定的应用程序上,为了标示这些应用程序,就会给这些应用程序进


行数字标示。为了方便称呼这些数字,就定义为端口。
3、定义通讯规则,这个规则就成为协议。国际组织定义了一个通用协议TCP/IP。


IP地址:
网络中设备的标识
不易记忆
本地回环地址:127.0.0.1 主机名:localhost


Demo:
import java.net.*;
class IPDemo{
public static void main(String[] args) throws Exception{
InetAddress i = InetAddress.getLocalHost();
System.out.println(i.toString());

InetAddress ia = InetAddress.getByName("www.baidu.com");
System.out.println(ia.getHostName());
}
}


两种通讯协议的区别:


UDP:
将数据及源和目的封装成数据包中,不需要建立连接
每个数据报的大小限制在64K内
因无连接,是不可靠协议
不需要建立连接,速度快


TCP:
建立连接,形成传输数据的通道。
在连接中进行大数据量传输
通过三次握手完成连接,是可靠协议
必须建立连接,效率会稍低




Socket编程:


Socket就是网络中为网络服务提供的一种机制
通信的两端都有socket
网络通信其实就是Socket间的通信
数据在两个Socket间通过IO传输


UDP的Socket服务建立:
1.建立udpsocket服务
2.提供数据,并将数据封装到数据包中
3.通过socket服务的发送功能将数据包发送出去
4.关闭资源  
Demo:


import java.net.*;
class UdpSent{
public static void main(String[] args)throws Exception{
DatagramSocket ds = new DatagramSocket();


byte[] data = "UDP数据".getBytes();


DatagramPacket dp = new DatagramPacket


(data,data.length,InetAddress.getByName(127.0.0.1),1000);

ds.send(dp);

ds.close();

}
}


class UdpRece{
public static void main(String[] args){
/*
1.定义udpsocket
2.定义一个数据包,用来接收收到的数据包
3.通过socket服务的接收功能,将收到的数据存到数据包中
4.通过数据包特有的功能将数据取出,并打印显示
5.关闭资源
*/
DatagramSocket ds = new DatagramSocket();


byte[] buf = new byte[1024];


DatagramPacket dp = new DatagramPacket(buf,buf.length);


ds.receive(dp);

String data = new String(dp.getData(),0,dp.getLength());


int port = dp.getPort();


String ip = dp.getAddress().getHostAddress();


ds.close();


System.out.println(data+"   "+port+"    "+ip);
}
}
TCP的Socket服务建立:


1.创建Socket服务,并指定要连接的主机和端口


import java.io.*;
import java.net.*;
class TcpClient
{
public static void main(String[] args)throws Exception{
Socket s = new Socket("192.168.0.107",10001);
OutputStream out = s.getOutputStream();
out.write("TCP协议通讯客户端数据!");
s.close();
}
}


class TcpServer
{
public static void main(String[] args)throws Exception{

ServerSocket ss = new ServerSocket(10001);
Socket s =ss.accept();
InputStream in = s.getInputStream();
byte[] buf = new byte[1000];
int len = in.read(buf);
System.out.println(new String(buf,0,len));
}
}




文件传输Demo


import java.io.*;
import java.net.*;
class TextClient
{
public static void main(String[] args)throws Exception{
Socket s = new Socket("192.168.0.107",10001);
BufferedReader bufr = 
new BufferedReader(new FileReader("UdpDemo.java"));
PrintWriter out = new PrintWriter(s.getOutputStream(),true);
String line = null;
while((line = bufr.readLine())!=null){
out.println(line);
}
s.shutdownOutput();
InputStream in = s.getInputStream();
byte[] buf = new byte[1024];
int len = in.read(buf);
System.out.println(new String(buf,0,len));
s.close();
}
}


class TextServer
{
public static void main(String[] args)throws Exception{

ServerSocket ss = new ServerSocket(10001);


Socket s =ss.accept();


BufferedReader bufr = new BufferedReader(new InputStream


(s.getInputStream()));


PrintWriter out = new PrintWriter(new FileWriter("server.txt"));


String line = null;


While((line = bufr.readLine())!=null){


out.println(line);


}
out.close();//缺少该语句将导致PrintWriter流无法刷新,文件中没有


数据。
OutputStream os = s.getOutputStream();
os.write("文件上传成功".getByte());
os.close();
s.close();
ss.close();
}
}





0 0