1、每一个网络应用程序都有一个数字标识,被成为端口。该端口是一个逻辑端口。端口范围是0~65535.其中0~1024为系统使用或保留端口
2、通信协议tcp/ip
3、127.0.0.1本地回环地址,本机为配备任何ip地址时,默认的ip地址就是127.0.0.1
4、数据封包---------数据拆包
5、ip地址是网络中设备的标识。
6、java中用来描述ip的类是InetAddress,该类没有构造函数。示例如下:
获取本机的ip和主机名
InetAddress ia = InetAddress.getLocalHost();
System.out.println(ia.toString());
System.out.println(ia.getHostAddress());
System.out.println(ia.getHostName());
获取网络上某台主机的ip地址:
InetAddress[] ia = InetAddress.getAllByName("baidu.com");
for( InetAddress i: ia )
{
System.out.println(i.getHostAddress());
System.out.println(i.getHostName());
}
打印结果是:
220.181.111.86
baidu.com
123.125.114.144
baidu.com
220.181.111.85
baidu.com
7、UDP:面向无连接,每个数据报要求在64k以内,不可靠但是速度快;
TCP:面向连接,形成数据通道,可进行大数据量的传输,可靠但效率低一点儿。
8、java中的网络通信就是socket通信。
9、udp发送端代码(不含异常处理)
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class UDPSender {
/**
* 做一个发送端的应用程序
*/
public static void main(String[] args)throws Exception {
// TODO Auto-generated method stub
//1、创建一个具有发送功能的套接字
DatagramSocket ds = new DatagramSocket();
//2、封装要发送的数据报
byte[] data = "hello ,udp and java".getBytes();
DatagramPacket dp = new DatagramPacket(data,data.length,InetAddress.getByName("192.168.229.1"),10000);
//3、调用套接字对象的发送功能,将数据报发送出去。
ds.send(dp);
//4、关闭资源
ds.close();
}
}
udp接收端代码:
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class UDPReceiver {
/**
* 做一个接受端的应用程序
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
//1、创建接收端的套接字对象;
DatagramSocket ds = new DatagramSocket(10000);
//2、创建一数据报对象,用来接受数据。
byte[] data = new byte[1024];
DatagramPacket dp = new DatagramPacket(data,data.length);
//3、使用该接收端套接字对象的方法接受数据报包
ds.receive(dp);
//4、显示该数据
System.out.println(dp.getAddress().getHostAddress()+"....."+
new String(dp.getData(),0,dp.getLength()));
//5、关闭资源
ds.close();
}
}
10、血泪的教训:
缓冲流,请刷新!
11、客户端上传文本,在服务端保存。
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
public class SocketCopyTxtDemo {
}
class TcpClient
{
public static void main(String[] args)
{
Socket s = null;
BufferedReader br = null;
try {
s = new Socket("192.168.0.101",10001);
br = new BufferedReader(new FileReader("D:\\望月.txt"));
BufferedWriter bufw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
String line;
while((line = br.readLine()) != null )
{
System.out.println(line);
bufw.write(line);
bufw.newLine();
bufw.flush();
}
//客户端停止输出
s.shutdownOutput();
BufferedReader bufr = new BufferedReader(new InputStreamReader(s.getInputStream()));
System.out.println(bufr.readLine());
} catch (UnknownHostException e) {
// TODO Auto-generated catch blocke.printStackTrace();
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
if( s != null )
{
try {
s.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if( br != null )
{
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
class TcpServer
{
public static void main(String[] args)
{
ServerSocket ss = null;
BufferedWriter bw = null;
try {
ss = new ServerSocket(10001);
Socket s = ss.accept();
BufferedReader bufr = new BufferedReader(new InputStreamReader(s.getInputStream()));
bw = new BufferedWriter(new FileWriter(
"C:\\Documents and Settings\\Administrator\\My Documents\\Wandoujia2\\望月.txt"));
String line;
while( (line = bufr.readLine()) != null )
{
bw.write(line);
bw.newLine();
bw.flush();
}
BufferedWriter bufw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
bufw.write("服务端:我已接收完毕!");
bufw.newLine();
bufw.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
if( ss != null )
{
try {
ss.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if( bw != null )
{
try {
bw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
该代码需要注意的是,
在使用缓冲流的readLine方法时,如果读取的是文本文件,这没啥说的,如果读取的是流中文本数据时,记得在数据末尾加入换行,然后刷新!