Java的Socket編程
package aaa;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
public class Sample extends Thread
{
private Socket s;
public Sample(Socket s)
{
this.s=s;
}
public void run()
{
try {
OutputStream os = s.getOutputStream();
InputStream is = s.getInputStream();
os.write("Hello,welcome you!".getBytes());
byte[] buf = new byte[100];
int len=is.read(buf);
System.out.println(new String(buf, 0, len));
os.close();
is.close();
s.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args)
{
if(args.length>0) //配置一下運(yùn)行方式,當(dāng)有參數(shù)傳入時(shí)運(yùn)行server()方法
server();
else //否則,運(yùn)行client()方法
client();
}
public static void server()
{
try {
ServerSocket ss = new ServerSocket(6000);
while(true){
Socket s=ss.accept();
new Sample(s).start();
//ss.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void client()
{
try {
Socket s = new Socket(InetAddress.getByName(null), 6000);
OutputStream os = s.getOutputStream();
InputStream is = s.getInputStream();
byte[] buf = new byte[100];
int len = is.read(buf);
System.out.println(new String(buf, 0, len));
os.write("Hello,this is quan!".getBytes());
os.close();
is.close();
s.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
|
三種方式獲取本機(jī)IP地址: InetAddress.getByName(null) InetAddress.getByName("localhost") InetAddress.getByName("127.0.0.1")
以上是基于TCP的socket編程。其中 服務(wù)器程序編寫: 1、調(diào)用ServerSocket(int port)創(chuàng)建一個(gè)服務(wù)器端套接字,并綁定到指定的端口上; 2、調(diào)用accept(),監(jiān)聽連接請(qǐng)求,如果客戶端請(qǐng)求連接,則接受連接,返回通信套接字; 3、調(diào)用Socket類的getOutputStream()和getInputStream()獲取輸出流和輸入流,開始網(wǎng)絡(luò)數(shù)據(jù)的發(fā)送和接收; 4、最后關(guān)閉通信套接字。
客戶端程序編寫: 1、調(diào)用Socket()創(chuàng)建一個(gè)流套接字,并連接服務(wù)器端; 2、調(diào)用Socket類的getOutputStream()和getInputStream()獲取輸出流和輸入流,開始網(wǎng)絡(luò)數(shù)據(jù)的發(fā)送和接收; 3、最后關(guān)閉通信套接字。
|
package aaa;
import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.net.SocketException; import java.net.UnknownHostException;
public class Sample extends Thread { public static void main(String[] args) { if(args.length>0) recive(); else send(); } public static void recive() { try { DatagramSocket ds = new DatagramSocket(6000); byte[] buf = new byte[100]; DatagramPacket dp = new DatagramPacket(buf, 100); ds.receive(dp); System.out.println(new String(buf, 0, dp.getLength())); String str = "Welcome you!"; DatagramPacket dpSend = new DatagramPacket(str.getBytes(), str.length(), dp.getAddress(), dp.getPort()); ds.send(dpSend); ds.close(); } catch (SocketException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
public static void send() { try { DatagramSocket ds=new DatagramSocket(); String str = "Hello,this is yihan!"; DatagramPacket dp = new DatagramPacket(str.getBytes(), str.length(), InetAddress.getByName("localhost"), 6000); ds.send(dp); byte[] buf = new byte[100]; DatagramPacket dpRecv = new DatagramPacket(buf, 100); ds.receive(dpRecv); System.out.println(new String(buf,0,dpRecv.getLength())); ds.close(); } catch (SocketException e) { e.printStackTrace(); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
|
基于UDP的socket編程 接收端程序編寫: 1、調(diào)用DatagramSocket(int port)創(chuàng)建一個(gè)數(shù)據(jù)報(bào)套接字,并綁定到指定端口上; 2、調(diào)用DatagramPacket(byte[] buf,int length),建立一個(gè)字節(jié)數(shù)組以接收UDP包; 3、調(diào)用Datagramsocket類的receive(),接收UDP包; 4、最后關(guān)閉數(shù)據(jù)報(bào)套接字;
發(fā)送端程序編寫: 1、調(diào)用DatagramSocket()創(chuàng)建一個(gè)數(shù)據(jù)報(bào)套接字; 2、調(diào)用DatagramPacket(byte[] buf,int offset,int length,InetAddress address,int port),建立要
發(fā)送的UDP包; 3、調(diào)用DatagramSocket類的send(),發(fā)送UDP包; 4、最后關(guān)閉數(shù)據(jù)報(bào)套接字。
|