java6-网络通信
1,几个常用类
- 服务器端socket:
ServerSocket
- 客户端socket:
Socket
- IP地址:
InetAddress
2,简单的聊天对话系统
//服务器端
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
public class simpleServer {
public static final int SERVER_PORT = 40000;
public static final Charset SERVER_CHARSET = StandardCharsets.UTF_8;
public static final String BYE = "bye";
public static void main(String[] args) throws IOException {
talkWithClient();
}
private static void talkWithClient() throws IOException {
System.out.println("Server启动,在端口 " + SERVER_PORT + " 监听。。。");
try(
ServerSocket ss = new ServerSocket(SERVER_PORT); //监听端口
Socket s = ss.accept(); //建立连接
){
Chat chat = new Chat("客户端", "成功建立连接", s);
chat.chatting();
}catch (Exception e){
e.printStackTrace();
}
}
}
//客户端
import java.io.IOException;
import java.net.Socket;
public class simpleClinet {
public static void main(String[] args) throws IOException {
talkWithServer();
}
private static void talkWithServer() throws IOException {
try(
//localhost == 127.0.0.1 == 本机IP
Socket socket = new Socket("localhost", simpleServer.SERVER_PORT);
){
Chat chat = new Chat("服务器端", null, socket);
chat.chatting();
} catch (Exception e){
e.printStackTrace();
}
}
}
//对话类
import java.io.*;
import java.net.Socket;
import java.util.Scanner;
public class Chat {
private String from;
private String greetings;
private Socket socket;
public Chat(String from, String greetings, Socket socket){
this.from = from;
this.greetings = greetings;
this.socket = socket;
}
public void chatting() throws IOException {
Scanner sc = new Scanner(System.in);
try(
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream(), simpleServer.SERVER_CHARSET));
PrintWriter pw = new PrintWriter(new OutputStreamWriter(socket.getOutputStream(), simpleServer.SERVER_CHARSET));
){
System.out.println("Socket连接成功!");
if(greetings!=null){
pw.println("你好," + from + "。 " + greetings);
pw.flush();
}
while (true){
String line = br.readLine();
if(line.trim().equalsIgnoreCase(simpleServer.BYE)){
System.out.println("对方要求断开连接!");
pw.println(simpleServer.BYE);
pw.flush();
break;
}else{
System.out.println("来自 "+from+" 的消息:"+line);
}
line = sc.nextLine();
pw.println(line);
pw.flush();
}
}catch (Exception e){
e.printStackTrace();
}
System.out.println("聊天结束。");
}
}
3,简单的聊天系统-V2-不用chat类
//服务器端
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;
public class simpleServer {
public static final int SERVER_PORT = 40000;
public static final Charset SERVER_CHARSET = StandardCharsets.UTF_8;
public static final String BYE = "bye";
public static void main(String[] args) throws IOException {
talkWithClient();
}
static void talkWithClient() throws IOException {
try(
ServerSocket ss = new ServerSocket(SERVER_PORT);
Socket s = ss.accept();
BufferedReader br = new BufferedReader(
new InputStreamReader(s.getInputStream(), SERVER_CHARSET));
PrintWriter pw = new PrintWriter(
new OutputStreamWriter(s.getOutputStream(), SERVER_CHARSET));
Scanner sc = new Scanner(System.in);
){
System.out.println("成功建立连接!" + s.getRemoteSocketAddress());
while(true){
String line = br.readLine();
if(line.trim().equalsIgnoreCase(BYE)){
pw.println(BYE);
pw.flush();
System.out.println("断开连接!");
break;
}else{
System.out.println(line);
pw.println(sc.nextLine());
pw.flush();
}
}
}catch (Exception e){
e.printStackTrace();
}
}
}
//客户端
import java.io.*;
import java.net.Socket;
import java.util.Scanner;
public class simpleClinet {
public static void main(String[] args) throws IOException {
talkWithServer();
}
static void talkWithServer() throws IOException {
try(
Socket s = new Socket("localhost", simpleServer.SERVER_PORT);
BufferedReader br = new BufferedReader(
new InputStreamReader(s.getInputStream(), simpleServer.SERVER_CHARSET));
PrintWriter pw = new PrintWriter(
new OutputStreamWriter(s.getOutputStream(), simpleServer.SERVER_CHARSET));
Scanner sc = new Scanner(System.in);
){
System.out.println("链接到服务器:"+s.getRemoteSocketAddress());
while (true){
pw.println(sc.nextLine());
pw.flush();
String line = br.readLine();
if(line.trim().equalsIgnoreCase(simpleServer.BYE)){
pw.println(simpleServer.BYE);
pw.flush();
System.out.println("断开连接!");
break;
}else {
System.out.println(line);
}
}
}catch (Exception e){
e.printStackTrace();
}
}
}
4,简单抓取网页内容
import java.io.*;
import java.net.*;
import java.nio.charset.StandardCharsets;
public class getHtmlContentAppMain {
public static void main(String[] args) throws IOException {
//域名解析
InetAddress inetAddress = InetAddress.getByName("www.baidu.com");
System.out.println("网站IP地址为:"+inetAddress);
//连接服务器
Socket s = new Socket();
SocketAddress sa = new InetSocketAddress(inetAddress, 80);
s.connect(sa, 10000); //10000为连接时限
System.out.println("连接到服务器");
//发送HTTP请求
PrintWriter pw = new PrintWriter(
new OutputStreamWriter(s.getOutputStream(), StandardCharsets.UTF_8));
StringBuffer sb = new StringBuffer();
sb.append("GET /index.html HTTP/1.1\r\n");
sb.append("Host: www.baidu.com\r\n");
sb.append("Connection: Keep-Alive\r\n");
sb.append("\r\n");
pw.println(sb.toString());
pw.flush();
//接收返回值
BufferedReader br = new BufferedReader(
new InputStreamReader(s.getInputStream(), StandardCharsets.UTF_8));
br.lines().forEach(System.out::println);
}
}
行动是治愈恐惧的良药,而犹豫拖延将不断滋养恐惧。