功能:客戶端發(fā)送一句話到服務(wù)器:(TCP)
功能:客戶端發(fā)送一句話到服務(wù)器:(單向通信)(TCP)
客戶端:
package com.gao.Project.pro3;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
public class TestClient {//客戶端
public static void main(String[] args) throws IOException {
//1.創(chuàng)建套接字:指定服務(wù)器的IP和端口號
Socket s = new Socket("192.168.2.142",8888);
//對于程序員來說,向外發(fā)送數(shù)據(jù) 感受--->利用輸出流
OutputStream os = s.getOutputStream();
DataOutputStream dos = new DataOutputStream(os);
//利用這個(gè)OutputStream就可以向外發(fā)送數(shù)據(jù)了,但是沒有直接發(fā)送String 的方法
//所以我們又在OutputStream外面套了一個(gè)處理流DataOutputStream
dos.writeUTF("你好");
//3.關(guān)閉流 + 關(guān)閉網(wǎng)絡(luò)資源:
dos.close();
os.close();
s.close();
}
}
服務(wù)器:
package com.gao.Project.pro3;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class TestServer {//服務(wù)器
public static void main(String[] args) throws IOException {
//1.創(chuàng)建套接字:指定服務(wù)器的端口號
ServerSocket ss = new ServerSocket(8888);
//2.等著客戶端發(fā)來的信息:
Socket s = ss.accept();//阻塞方法:等待接收客戶端的數(shù)據(jù),什么時(shí)候收到數(shù)據(jù),什么時(shí)候程序繼續(xù)向下執(zhí)行
//accept()返回值為一個(gè)Socket,這個(gè)Socket其實(shí)就是客戶端的Socket
//接到這個(gè)Socket以后,客戶端和服務(wù)器才真正產(chǎn)生了連接,才真正可以通信了
//3.感受到的操作流:
InputStream is = s.getInputStream();
DataInputStream dis = new DataInputStream(is);
//4.讀取客戶端發(fā)來的數(shù)據(jù)
String str = dis.readUTF();
System.out.println("客戶端發(fā)來的數(shù)據(jù)為"+str);
//5.關(guān)閉流+關(guān)閉網(wǎng)絡(luò)資源:
dis.close();
is.close();
s.close();
ss.close();
}
}
測試:
(1)先開服務(wù)器,再開啟啟客戶端
(2)先開客戶端,就會出錯(cuò)
功能:服務(wù)器端接收完客戶端發(fā)來的數(shù)據(jù),再向客戶端輸出一句話:(雙向通信)(TCP)
服務(wù)器:
package com.gao.Project.pro3;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class TestServer {//服務(wù)器
public static void main(String[] args) throws IOException {
//1.創(chuàng)建套接字:指定服務(wù)器的端口號
ServerSocket ss = new ServerSocket(8888);
//2.等著客戶端發(fā)來的信息:
Socket s = ss.accept();//阻塞方法:等待接收客戶端的數(shù)據(jù),什么時(shí)候收到數(shù)據(jù),什么時(shí)候程序繼續(xù)向下執(zhí)行
//accept()返回值為一個(gè)Socket,這個(gè)Socket其實(shí)就是客戶端的Socket
//接到這個(gè)Socket以后,客戶端和服務(wù)器才真正產(chǎn)生了連接,才真正可以通信了
//3.感受到的操作流:
InputStream is = s.getInputStream();
DataInputStream dis = new DataInputStream(is);
//4.讀取客戶端發(fā)來的數(shù)據(jù)
String str = dis.readUTF();
System.out.println("客戶端發(fā)來的數(shù)據(jù)為"+str);
//向客戶端輸出一句話:---->操作流---->輸出流
OutputStream os = s.getOutputStream();
DataOutputStream dos =new DataOutputStream(os);
dos.writeUTF("你好,我是服務(wù)器端,我接收到你的請求了");
//5.關(guān)閉流+關(guān)閉網(wǎng)絡(luò)資源:
dis.close();
is.close();
s.close();
ss.close();
}
}
客戶端:
package com.gao.Project.pro3;
import java.io.*;
import java.net.Socket;
public class TestClient {//客戶端
public static void main(String[] args) throws IOException {
//1.創(chuàng)建套接字:指定服務(wù)器的IP和端口號
Socket s = new Socket("192.168.2.142",8888);
//對于程序員來說,向外發(fā)送數(shù)據(jù) 感受--->利用輸出流
OutputStream os = s.getOutputStream();
DataOutputStream dos = new DataOutputStream(os);
//利用這個(gè)OutputStream就可以向外發(fā)送數(shù)據(jù)了,但是沒有直接發(fā)送String 的方法
//所以我們又在OutputStream外面套了一個(gè)處理流DataOutputStream
dos.writeUTF("你好");
//接收服務(wù)器端的回話--->利用輸入流:
InputStream is = s.getInputStream();
DataInputStream dis = new DataInputStream(is);
String str = dis.readUTF();
System.out.println("服務(wù)器對我說"+str);
//3.關(guān)閉流 + 關(guān)閉網(wǎng)絡(luò)資源:
dis.close();
is.close();
dos.close();
os.close();
s.close();
}
}

浙公網(wǎng)安備 33010602011771號