Java 网络编程
基于TCP协议的Socket网络通信,用来实现双向安全连接网络通信。
Socket通信模型
进行网络通信时,Socket需要借助数据流来完成数据的传递工作
TCP
public class IntAddress {
public static void main(String[] args) {
try {
InetAddress localHost = InetAddress.getLocalHost();
System.out.println(localHost);
InetAddress byName = InetAddress.getByName("www.baidu.com");
System.out.println(byName);
System.out.println(byName.getHostAddress());
System.out.println(byName.getHostName());
InetAddress byName1 = InetAddress.getByName("14.215.177.38");
System.out.println(byName1);
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
public class Server {
public static void main(String[] args) {
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(8000);
Socket accept = serverSocket.accept();
InputStream inputStream = accept.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String line = null;
while ((line = bufferedReader.readLine()) != null)
System.out.println(line);
serverSocket.close();
accept.close();
inputStream.close();
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class Client {
public static void main(String[] args) {
try {
Socket socket = new Socket("localhost",8000);
OutputStream outputStream = socket.getOutputStream();
String info="你好吗?";
outputStream.write(info.getBytes());
socket.close();
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class User implements Serializable {
private String userName;
private String password;
public User() {
}
public User(String userName, String password) {
this.userName = userName;
this.password = password;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User{" +
"userName='" + userName + '\'' +
", password='" + password + '\'' +
'}';
}
}
public class Server {
public static void main(String[] args) {
try {
ServerSocket serverSocket = new ServerSocket(9000);
Socket accept = serverSocket.accept();
InputStream inputStream = accept.getInputStream();
ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);
User user=(User)objectInputStream.readObject();
System.out.println("客户端发来的信息是:"+user);
String line="登录成功";
OutputStream outputStream = accept.getOutputStream();
outputStream.write(line.getBytes());
serverSocket.close();
accept.close();
inputStream.close();
objectInputStream.close();
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
public class Client {
public static void main(String[] args) {
try {
Socket socket = new Socket("127.0.0.1",9000);
OutputStream outputStream = socket.getOutputStream();
ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
objectOutputStream.writeObject(new User("小明","123456"));
InputStream inputStream = socket.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String line=null;
while ((line=bufferedReader.readLine())!=null)
System.out.println("服务端发来的信息:"+line);
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class User implements Serializable {
private String userName;
private String password;
public User() {
}
public User(String userName, String password) {
this.userName = userName;
this.password = password;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User{" +
"userName='" + userName + '\'' +
", password='" + password + '\'' +
'}';
}
}
public class Client1 {
public static void main(String[] args) {
try {
Socket socket = new Socket("localhost",11000);
OutputStream outputStream = socket.getOutputStream();
ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
objectOutputStream.writeObject(new User("小明","123456"));
InputStream inputStream = socket.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String info=null;
while ((info=bufferedReader.readLine())!=null)
System.out.println("服务器发来的消息:"+info);
socket.close();
outputStream.close();
objectOutputStream.close();
inputStream.close();
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class Client2 {
public static void main(String[] args) {
try {
Socket socket = new Socket("localhost",11000);
OutputStream outputStream = socket.getOutputStream();
ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
objectOutputStream.writeObject(new User("小李","111111"));
InputStream inputStream = socket.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String info=null;
while ((info=bufferedReader.readLine())!=null)
System.out.println("服务器发来的消息:"+info);
socket.close();
outputStream.close();
objectOutputStream.close();
inputStream.close();
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class Server {
public static void main(String[] args) {
try {
ServerSocket serverSocket = new ServerSocket(11000);
while(true)
{
Socket accept = serverSocket.accept();
ThreadDemo threadDemo = new ThreadDemo(accept);
threadDemo.start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class ThreadDemo extends Thread{
Socket accpect=null;
public ThreadDemo(Socket accpect){
this.accpect=accpect;
}
@Override
public void run() {
try {
InputStream inputStream = accpect.getInputStream();
ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);
User user=(User)objectInputStream.readObject();
System.out.println("客户端发来的消息:"+user);
String line="欢迎你,"+user.getUserName()+",登录成功";
OutputStream outputStream = accpect.getOutputStream();
outputStream.write(line.getBytes());
accpect.close();
objectInputStream.close();
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
UDP
public class UDPClient {
public static void main(String[] args) throws Exception {
String line="你好";
InetAddress localHost = InetAddress.getLocalHost();
DatagramPacket datagramPacket = new DatagramPacket(line.getBytes(),line.getBytes().length,localHost,9000);
DatagramSocket socket = new DatagramSocket();
socket.send(datagramPacket);
byte[] bytes = new byte[1024];
DatagramPacket datagramPacket1 = new DatagramPacket(bytes,bytes.length);
socket.receive(datagramPacket1);
System.out.println(new String(bytes));
socket.close();
}
}
public class UDPServer {
public static void main(String[] args) throws Exception {
DatagramSocket socket = new DatagramSocket(9000);
byte[] bytes = new byte[1024];
DatagramPacket datagramPacket = new DatagramPacket(bytes,bytes.length);
socket.receive(datagramPacket);
System.out.println(new String(bytes));
String info = "收到";
DatagramPacket datagramPacket1 = new DatagramPacket(info.getBytes(),info.getBytes().length,datagramPacket.getSocketAddress());
socket.send(datagramPacket1);
socket.close();
}
}
|