java net
阿新 • • 發佈:2020-07-14
InetAddress 代表 IP
import java.net.InetAddress; import java.net.UnknownHostException; public class Net1 { public static void main(String[] args) { InetAddress inet1 = null; InetAddress inet2 = null; try { inet1 = InetAddress.getByName("192.168.5.76"); // the output is: /192.168.5.76 inet2 = InetAddress.getByName("www.baidu.com"); // the output is: www.baidu.com/104.193.88.123 } catch (UnknownHostException e) { e.printStackTrace(); } System.out.println(inet1); System.out.println(inet2); } }
An Example
Client
@Test public void testClient(){ Socket socket = null; OutputStream os = null; try { InetAddress inet = InetAddress.getByName("127.0.0.1"); socket = new Socket(inet, 22250); os = socket.getOutputStream(); os.write("hello".getBytes()); } catch (IOException e) { e.printStackTrace(); } finally { if(os != null){ try { os.close(); } catch (IOException e) { e.printStackTrace(); } } if(socket != null){ try { socket.close(); } catch (IOException e) { e.printStackTrace(); } } } }
Server
@Test public void testServer(){ ServerSocket ss = null; Socket accept = null; InputStream is = null; ByteArrayOutputStream baos = null; try { ss = new ServerSocket(22250); accept = ss.accept(); // this step get the socket of the client is = accept.getInputStream(); // byte[] buf = new byte[1024]; // int len; // while((len = is.read(buf)) != -1){ // String str = new String(buf, 0, len); // System.out.println(str); // } baos = new ByteArrayOutputStream(); byte[] buffer = new byte[5]; int len; while((len = is.read(buffer)) != -1){ baos.write(buffer, 0, len); } System.out.println(baos.toString()); } catch (IOException e) { e.printStackTrace(); } finally { if(is != null){ try { is.close(); } catch (IOException e) { e.printStackTrace(); } } if(baos != null){ try { baos.close(); } catch (IOException e) { e.printStackTrace(); } } if(accept != null){ try { accept.close(); } catch (IOException e) { e.printStackTrace(); } } if(ss != null){ try { ss.close(); } catch (IOException e) { e.printStackTrace(); } } } }
This time send a FILE
the Client
@Test
public void testClient2(){
Socket socket = null;
BufferedInputStream bis = null;
OutputStream os = null;
try {
InetAddress inet = InetAddress.getByName("127.0.0.1");
socket = new Socket(inet, 22250);
bis = new BufferedInputStream(new FileInputStream(new File("/Users/zhuya/Pictures/Lady.jpg")));
os = socket.getOutputStream();
byte[] buf = new byte[1024];
int len;
while ((len = bis.read(buf)) != -1){
os.write(buf,0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
the Server
@Test
public void testServer2(){
ServerSocket ss = null;
Socket socket = null;
InputStream is = null;
BufferedOutputStream bos = null;
try {
ss = new ServerSocket(22250);
socket = ss.accept();
is = socket.getInputStream();
bos = new BufferedOutputStream(new FileOutputStream(new File("/Users/zhuya/Desktop/lady.jpg")));
int len;
byte[] buf = new byte[1024];
while ((len = is.read(buf)) != -1){
bos.write(buf, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bos != null) {
try {
bos.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 (ss != null) {
try {
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
UDP example
sender
@Test
public void sender(){
DatagramSocket socket = null;
try {
socket = new DatagramSocket();
// watch the packet
String str = "This is UDP speaking";
InetAddress inet = InetAddress.getByName("127.0.0.1");
DatagramPacket packet = new DatagramPacket(str.getBytes(), 0, str.getBytes().length, inet, 22250);
socket.send(packet);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (socket != null) {
socket.close();
}
}
}
receiver
@Test
public void receiver(){
DatagramSocket socket = null;
try {
socket = new DatagramSocket(22250);
byte[] buffer = new byte[100];
DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length);
socket.receive(packet);
System.out.println(new String(packet.getData(), 0, packet.getLength()));
} catch (IOException e) {
e.printStackTrace();
} finally {
if (socket != null) {
socket.close();
}
}
}
URL
@Test
public void URLTest(){
URL url = null;
try {
url = new URL("http://www.baidu.com/search_result.jsp?search_id=3731&search_type=search_videos");
} catch (MalformedURLException e) {
e.printStackTrace();
}
System.out.println(url.getProtocol());
System.out.println(url.getPort());
System.out.println(url.getHost());
System.out.println(url.getPath());
System.out.println(url.getFile());
System.out.println(url.getQuery());
}