package internet.lesson01;
import java.net.InetSocketAddress;
public class InetSocketAdressTest {
public static void main(String[] args) {
InetSocketAddress inetSocketAddress1=new InetSocketAddress("127.0.0.1",8080);
InetSocketAddress inetSocketAddress2=new InetSocketAddress("localhost",8080);
System.out.println(inetSocketAddress1);
System.out.println(inetSocketAddress2);
System.out.println(inetSocketAddress1.getAddress());
System.out.println(inetSocketAddress1.getHostName());
System.out.println(inetSocketAddress1.getPort());
}
}
package internet.lesson01;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class InetTest {
public static void main(String[] args) {
try {
InetAddress inetAddress1=InetAddress.getByName("127.0.0.1");
System.out.println(inetAddress1);
InetAddress inetAddress3=InetAddress.getByName("localhost");
System.out.println(inetAddress3);
InetAddress inetAddress4=InetAddress.getLocalHost();
System.out.println(inetAddress4);
InetAddress inetAddress2=InetAddress.getByName("www.baidu.com");
System.out.println(inetAddress2);
System.out.println(inetAddress2.getCanonicalHostName());
System.out.println(inetAddress2.getHostAddress());
System.out.println(inetAddress2.getHostName());
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
TCP传输接收数据:
package internet.lesson02;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class TCPServerDemo01 {
public static void main(String[] args) {
ServerSocket serverSocket=null;
Socket socket=null;
InputStream iStream=null;
ByteArrayOutputStream baos=null;
try {
serverSocket=new ServerSocket(9999);
socket=serverSocket.accept();
iStream=socket.getInputStream();
baos=new ByteArrayOutputStream();
byte[] b=new byte[1024];
int len;
while((len=iStream.read(b))!=-1) {
baos.write(b,0,len);
}
System.out.println(baos.toString());
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if(baos!=null) baos.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if(iStream != null) iStream.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if(socket!=null) socket.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if(serverSocket!=null) serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
package internet.lesson02;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
public class TCPClientDemo01 {
public static void main(String[] args) {
Socket socket=null;
OutputStream oStream=null;
try {
InetAddress serverIP= InetAddress.getByName("127.0.0.1");
int port=9999;
socket=new Socket(serverIP,port);
oStream= socket.getOutputStream();
oStream.write("你好".getBytes());
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if(oStream!=null) oStream.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if(socket!=null) socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
TCP图片下载:
package internet.lesson02;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class TCPServerDemo02 {
public static void main(String[] args) {
try {
ServerSocket serverSocket=new ServerSocket(9000);
Socket socket=serverSocket.accept();
InputStream is=socket.getInputStream();
FileOutputStream fos=new FileOutputStream(new File("receive.jpg"));
byte[] b=new byte[1024];
int len;
while((len=is.read(b))!=-1) {
fos.write(b,0,len);
}
fos.close();
is.close();
socket.close();
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
package internet.lesson02;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
public class TCPClientDemo02 {
public static void main(String[] args) {
try {
Socket socket=new Socket(InetAddress.getByName("127.0.0.1"),9000);
OutputStream os= socket.getOutputStream();
FileInputStream fin=new FileInputStream(new File("aaa.jpg"));
byte[] b=new byte[10];
int len;
while((len=fin.read(b))!=-1) {
os.write(b,0,len);
}
fin.close();
os.close();
socket.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
UDP:
package internet.lesson03;
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 UDPClientDemo01 {
public static void main(String[] args) {
try {
DatagramSocket socket=new DatagramSocket();
String msg="nihao";
InetAddress localhost=InetAddress.getByName("localhost");
int port=9090;
DatagramPacket packet=new DatagramPacket(msg.getBytes(), 0,msg.getBytes().length,localhost,port);
socket.send(packet);
socket.close();
} catch (SocketException e) {
e.printStackTrace();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
package internet.lesson03;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
public class UDPServerDemo01 {
public static void main(String[] args) {
try {
DatagramSocket socket=new DatagramSocket(9090);
byte[] b=new byte[100];
DatagramPacket packet=new DatagramPacket(b, 0,b.length);
socket.receive(packet);
System.out.println(new String(packet.getData()) );
socket.close();
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
|