网络编程-----TCP通信
🍎姓名:洋葱爱代码🍎
🍊喜欢:Java编程🍊
🍉重要的事情说三遍!!!🍉
🍓欢迎大家来访问哦,互相学习🍓
🍋欢迎大家来访问哦,互相学习🍋
🍑欢迎大家来访问哦,互相学习🍑
?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
🍎合抱之木,生于毫末;百丈之台,起于垒土;千里之行,始于足下。------《老子》 🍊今日学习任务!!!!! 🍊 1、掌握UDP通信
🔥一、开胃小菜阶段🍰
?TCP通信的自述?
???????????????????????????????????????????
🔥二、ServerSockect🍺
?ServerSockect的自述? 👌想拥有我吗?请先了解我的构造方法吧 📊在开发TCP程序时,首先需要创建服务器端程序。
???????????????????????????????????????????
🔥三、Sockect?
?Sockect对象的自述? ?Sockect类的构造方法? ?Sockect的常见用法?
???????????????????????????????????????????
🔥四、一个简单的TCP程序🌻
?客户端?
package socket;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.Socket;
public class Client {
public static void main(String[] args) throws IOException{
Socket client = new Socket(InetAddress.getLocalHost(), 8888);
InputStream inputStream = client.getInputStream();
byte[]buf =new byte[1024];
int length =inputStream.read(buf);
System.out.println(new String (buf , 0 , length));
client.close();
inputStream.close();
}
}
?服务端?
package socket;
import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) throws IOException, InterruptedException {
ServerSocket serverSocket = new ServerSocket(8888);
Socket client =serverSocket.accept();
OutputStream out = client.getOutputStream();
System.out.println("开始于客户端交互数据");
out.write("百度客服欢迎你!".getBytes());
Thread.sleep(5000);
System.out.println("结束与客户端交互数据");
out.close();
serverSocket.close();
client.close();
}
}
???????????????????????????????????????????
🔥五、多线程的TCP网络程序👓
客户端
package socket;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.Socket;
public class Client2 {
public static void main(String[] args) throws IOException{
Socket client = new Socket(InetAddress.getLocalHost(), 8888);
InputStream inputStream = client.getInputStream();
byte[]buf =new byte[1024];
int length =inputStream.read(buf);
System.out.println(new String (buf , 0 , length));
client.close();
inputStream.close();
}
}
服务端
package socket;
import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Server2 {
public static void main(String[] args) throws IOException, InterruptedException {
ServerSocket serverSocket = new ServerSocket(8888);
while (true) {
new Thread(new Runnable() {
@Override
public void run() {
Socket client = null ;
OutputStream out = null ;
try {
client =serverSocket.accept();
out = client.getOutputStream();
System.out.println("开始于客户端交互数据");
out.write("百度客服欢迎你!".getBytes());
Thread.sleep(5000);
System.out.println("结束与客户端交互数据");
}catch (Exception e) {
e.printStackTrace();
}finally {
if(out!=null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(out!=null) {
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(out!=null) {
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}).start();
}
}
}
???????????????????????????????????????????
🔥六、每日一夸🏆
🍑每日一夸!!! 🍑就如同薛之謙对毛不易夸奖的 🍑"你现在就是流星" , 但是我相信你 ,你坚持下去你就是巨星"
|