package com.www.java1;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class InetAdressTest {
public static void main(String[] args) {
try {
InetAddress name = InetAddress.getByName("192.168.10.14");
System.out.println(name);
InetAddress name1 = InetAddress.getByName("www.baidu.com");
System.out.println(name1);
InetAddress localHost = InetAddress.getLocalHost();
System.out.println(localHost);
System.out.println(name1.getHostName());
System.out.println(name.getHostAddress());
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
package com.www.java1;
import org.junit.Test;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
public class TCPTest1 {
@Test
public void client(){
Socket socket = null;
OutputStream outputStream = null;
try {
InetAddress host = InetAddress.getByName("127.0.0.1");
socket = new Socket(host, 8899);
outputStream = socket.getOutputStream();
outputStream.write("你好,我是客户端".getBytes());
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(socket != null)
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if(socket != null)
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Test
public void server(){
ServerSocket serverSocket = null;
Socket accept = null;
InputStream inputStream = null;
ByteArrayOutputStream bao = null;
try {
serverSocket = new ServerSocket(8899);
accept = serverSocket.accept();
inputStream = accept.getInputStream();
bao = new ByteArrayOutputStream();
byte[] bytes = new byte[5];
int len;
while((len = inputStream.read(bytes))!= -1){
bao.write(bytes,0, len);
}
System.out.println(bao.toString());
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(bao != null)
bao.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if(inputStream != null)
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if(accept != null)
accept.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if(serverSocket != null)
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
package com.www.java1;
import org.junit.Test;
import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
public class TCPTest2 {
@Test
public void client(){
FileInputStream filestream = null;
Socket socket = null;
OutputStream outputStream = null;
try {
File file = new File("hihello.txt");
filestream = new FileInputStream(file);
InetAddress host = InetAddress.getByName("127.0.0.1");
socket = new Socket(host, 8898);
outputStream = socket.getOutputStream();
byte[] bytes = new byte[12];
int len;
while((len = filestream.read(bytes))!= -1){
outputStream.write(bytes,0,len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(outputStream != null)
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if(socket != null)
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if(filestream != null)
filestream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Test
public void server(){
FileOutputStream fileOutputStream = null;
ServerSocket serverSocket = null;
Socket accept = null;
InputStream inputStream = null;
try {
fileOutputStream = new FileOutputStream(new File("lalala.txt"));
serverSocket = new ServerSocket(8898);
accept = serverSocket.accept();
inputStream = accept.getInputStream();
byte[] bytes = new byte[5];
int len;
while((len = inputStream.read(bytes)) != -1){
fileOutputStream.write(bytes, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(inputStream != null)
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if(accept != null)
accept.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if(serverSocket != null)
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if(fileOutputStream != null)
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
package com.www.java1;
import org.junit.Test;
import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
public class TCPTest3 {
@Test
public void client(){
FileInputStream filestream = null;
Socket socket = null;
OutputStream outputStream = null;
ServerSocket serverSocket = null;
ByteArrayOutputStream bao = null;
try {
File file = new File("hihelloha.txt");
filestream = new FileInputStream(file);
InetAddress host = InetAddress.getByName("127.0.0.1");
socket = new Socket(host, 8898);
outputStream = socket.getOutputStream();
byte[] bytes = new byte[12];
int len;
while((len = filestream.read(bytes))!= -1){
outputStream.write(bytes,0,len);
}
socket.shutdownOutput();
InputStream inputStream = socket.getInputStream();
bao = new ByteArrayOutputStream();
byte[] bytes1 = new byte[5];
int len1;
while((len1 = inputStream.read(bytes1))!= -1){
bao.write(bytes1,0, len1);
}
System.out.println(bao.toString());
} catch (IOException e) {
e.printStackTrace();
} finally {
try {if(bao != null)
bao.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if(outputStream != null) {
outputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if(socket != null) {
socket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if(filestream != null) {
filestream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Test
public void server(){
FileOutputStream fileOutputStream = null;
ServerSocket serverSocket = null;
Socket accept = null;
InputStream inputStream = null;
OutputStream outputStream = null;
try {
fileOutputStream = new FileOutputStream(new File("lalalala.txt"));
serverSocket = new ServerSocket(8898);
accept = serverSocket.accept();
inputStream = accept.getInputStream();
byte[] bytes = new byte[5];
int len;
while((len = inputStream.read(bytes)) != -1){
fileOutputStream.write(bytes, 0, len);
}
outputStream = accept.getOutputStream();
outputStream.write("接收成功".getBytes());
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(outputStream != null)
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if(inputStream != null)
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if(accept != null)
accept.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if(serverSocket != null)
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if(fileOutputStream != null)
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
package com.www.java1;
import org.junit.Test;
import javax.xml.crypto.Data;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class UDPTest {
@Test
public void send(){
DatagramSocket socket = new DatagramSocket();
String str = "你瞅啥?瞅你咋地!";
byte[] data = str.getBytes();
InetAddress localHost = InetAddress.getLocalHost();
DatagramPacket packet = new DatagramPacket(data,0,data.length,localHost,9090);
socket.send(packet);
socket.close();
}
@Test
public void recevie(){
DatagramSocket socket = new DatagramSocket(9090);
byte[] data = new byte[100];
DatagramPacket packet = new DatagramPacket(data,0,data.length);
socket.receive(packet);
System.out.println(new String(packet.getData(),0,packet.getLength()));
socket.close();
}
}
package com.www.java1;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class URLTest {
public static void main(String[] args) {
HttpURLConnection urlConnection = null;
InputStream inputStream = null;
FileOutputStream fileOutputStream = null;
try {
URL url = new URL("https://cn.bing.com/images/search?view=detailV2&ccid=AOdA8t0u&id=CA9B72EB873CA982050100C2E3D5E01C9BF4CFC6&thid=OIP.AOdA8t0ufnqy7JtKMYvMDQHaNK&mediaurl=https%3a%2f%2fuploadfile.bizhizu.cn%2fup%2fc7%2fcb%2f02%2fc7cb02e48dfecc2c1ce20baadc32af50.jpg&exph=1820&expw=1024&q=%e5%88%98%e4%ba%a6%e8%8f%b2&simid=608054175508419144&FORM=IRPRST&ck=DEE0576FC7DDC44BA2AEC6E8C29728A0&selectedIndex=0&ajaxhist=0&ajaxserp=0");
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.connect();
inputStream = urlConnection.getInputStream();
fileOutputStream = new FileOutputStream("Day10\\刘亦菲.jpeg");
byte[] bytes = new byte[1024];
int len;
while((len = inputStream.read(bytes)) != -1){
fileOutputStream.write(bytes, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(fileOutputStream != null)
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if(inputStream != null)
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
urlConnection.disconnect();
}
}
}
|