java 网络编程要点(二)
Socket和ServerSocket
ServerSocket 用于服务器端建立监听套接字
Socket用于建立直接与客服端进行通信的套接字
a)Socket类常用方法
Socket(String SeverhostName,int Serverport)==>连接到指定的服务器
Socket(InetAddress ServeripAdress,int Serverport)==》连接到指定的服务器
InetAdress getInetAdress()===>返回此套接字连接的远程IP
int getPort()===>返回此套接字连接的远程的端口
InetAddress getLocalAddress()===>返回此套接字本地的IP
int getLocalPort()===》返回此套接字绑定到的本地端口
InputStream getInputStream()===》返回此套接字的输入流
OutputStream getOutputStream()===》返回此套接字的输出流
void connect(SocketAddress ServerIP,int timeout)==>连接指定的服务器
boolean isConnect()==>判断此链接是否连接到对方服务器(true)
b)SeverSocket类常用方法
ServerSocket()==》创建一个非绑定的服务器套接字
ServerSocket(int port)==>创建一个绑定的服务器套接字
ServerSocket(int port,int backlog,InetAddress bindIP)==>创建一个绑定的服务器套接字
Socket accept()==>侦听并接受到此套接字的连接
close()
d)实例
public void testSocket()
{
try {
Socket mclient=new Socket("localhost",8114);
System.out.println(mclient.getLocalPort());
System.out.println(mclient.getLocalAddress());
System.out.println(mclient.getPort());
System.out.println(mclient.getInetAddress());
InputStream minput=mclient.getInputStream();
OutputStream mout=mclient.getOutputStream();
DataOutputStream moutput=new DataOutputStream(mout);
BufferedReader mbuff=new BufferedReader(new InputStreamReader(minput));
String line=null;
line=mbuff.readLine();
while(line!=null)
{
System.out.println(line);
line=mbuff.readLine();
}
String m_str="we are out!";
moutput.write(m_str.getBytes());
moutput.close();
mbuff.close();
minput.close();
mclient.close();
} catch (UnknownHostException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
public void testServerSocket()
{
try {
InetAddress mIP=InetAddress.getByName("localhost");
ServerSocket mServer=new ServerSocket(8114,5,mIP);
System.out.println(mServer.getLocalPort());
System.out.println(mServer.getInetAddress());
Socket msocket=mServer.accept();
while(msocket==null){System.out.println("等待连接!");msocket=mServer.accept();}
System.out.println(msocket.getLocalPort());
System.out.println(msocket.getLocalAddress());
System.out.println(msocket.getPort());
System.out.println(msocket.getInetAddress());
OutputStream mout=msocket.getOutputStream();
String m_str="welcome to come";
m_str+="\n";
m_str+="Ok!";
PrintStream mprint=new PrintStream(mout);
mprint.print(m_str.getBytes());
mprint.close();
msocket.close();
mServer.close();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
DatagramPacket 和DatagramSocket类
a)DatagramPacket用来指明将接收/发送的数据打包,
DatagramPacket (byte data[],int size)
DatagramPacket (byte data[],int offst,int size)
DatagramPacket (byte data[],int size,InetAddress ip,int port)==》目标地址和目标端口
DatagramPacket (byte data[],iint offset,nt size,InetAddress ip,int port)==》目标地址和目标端口
DatagramSocket用来发送、接收数据
DatagramSocket()==》绑定本主机
DatagramSocket(int port)==》绑定本主机
DatagramSocket(int port,InetAddress)==》绑定本主机
b)常用方法
DatagramSocket
send(DatagramPacket packet)
receive(DatagramSocket packet)
InetAddress getInetAddress()
int getLocalPort()
int getPort()
boolean isConnected()
DatagramSocket
InetAddress getAddress()
byte[] getData()
int getLength()
int getPort()
void setData(byte[]data)
c)实例
public void testDatapragram()
{
String str="welcome to come";
str+="\n";
str+="Ok!";
byte[] mbt=str.getBytes();
InetAddress mIP=null;
DatagramSocket msocket=null;
DatagramPacket mpra=null;
try {
mIP = InetAddress.getByName("localhost");
} catch (UnknownHostException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
mpra=new DatagramPacket(mbt, mbt.length,mIP,8115);
try {
msocket=new DatagramSocket(8116);
try {
msocket.send(mpra);
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
msocket.close();
} catch (SocketException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}