Java 網(wǎng)絡(luò)編程之 TCP 實(shí)現(xiàn)簡單的聊天系統(tǒng)
客戶端
1、連接服務(wù)器 Socket
2、發(fā)送消息
package lesson02;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
/**
* 客戶端
*/
public class TcpClientDemo1 {
public static void main(String[] args) {
Socket socket = null;
OutputStream os = null;
try {
//1、要知道服務(wù)器的地址 端口號
InetAddress serverIP = InetAddress.getByName("127.0.0.1");
int port = 9999;
//2、創(chuàng)建一個(gè) socket 連接
socket = new Socket(serverIP, port);
//3、發(fā)送消息 IO流
os = socket.getOutputStream();
os.write("你好,歡迎學(xué)習(xí)狂神學(xué)Java".getBytes());
} catch (Exception e) {
e.printStackTrace();
} finally {
if (os != null){
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket != null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
服務(wù)端
1、建立服務(wù)的端口 ServerSocket
2、等待用戶的連接 accept
3、接收用戶的消息
package lesson02;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
/**
* 服務(wù)端
*/
public class TcpServerDemo1 {
public static void main(String[] args) {
ServerSocket serverSocket = null;
Socket socket = null;
InputStream is = null;
ByteArrayOutputStream baos = null;
try {
//1、我得有一個(gè)地址
serverSocket = new ServerSocket(9999);
while (true){
//2、等待客戶端連接過來
socket = serverSocket.accept();
//3、讀取客戶端的消息
is = socket.getInputStream();
//管道流
baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len;
while((len = is.read(buffer)) != -1){
baos.write(buffer, 0 , len);
}
System.out.println(baos.toString());
}
/*
byte[] buffer = new byte[1024];
int len;
while ((len = is.read(buffer)) != -1){
String msg = new String(buffer, 0, len);
System.out.println(msg);
}
*/
} catch (IOException e) {
e.printStackTrace();
} finally {
//關(guān)閉資源
if (baos != null){
try {
baos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (is != null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket != null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (serverSocket != null){
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
服務(wù)端
1、建立服務(wù)的端口 ServerSocket
2、等待用戶的連接 accept
3、接收用戶的消息
package lesson02;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
/**
* 服務(wù)端
*/
public class TcpServerDemo1 {
public static void main(String[] args) {
ServerSocket serverSocket = null;
Socket socket = null;
InputStream is = null;
ByteArrayOutputStream baos = null;
try {
//1、我得有一個(gè)地址
serverSocket = new ServerSocket(9999);
while (true){
//2、等待客戶端連接過來
socket = serverSocket.accept();
//3、讀取客戶端的消息
is = socket.getInputStream();
//管道流
baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len;
while((len = is.read(buffer)) != -1){
baos.write(buffer, 0 , len);
}
System.out.println(baos.toString());
}
/*
byte[] buffer = new byte[1024];
int len;
while ((len = is.read(buffer)) != -1){
String msg = new String(buffer, 0, len);
System.out.println(msg);
}
*/
} catch (IOException e) {
e.printStackTrace();
} finally {
//關(guān)閉資源
if (baos != null){
try {
baos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (is != null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket != null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (serverSocket != null){
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
以上就是Java 網(wǎng)絡(luò)編程之 TCP 實(shí)現(xiàn)簡單的聊天系統(tǒng)的詳細(xì)內(nèi)容,更多關(guān)于Java 實(shí)現(xiàn)簡單的聊天系統(tǒng)的資料請關(guān)注腳本之家其它相關(guān)文章!
- 基于Java實(shí)現(xiàn)互聯(lián)網(wǎng)實(shí)時(shí)聊天系統(tǒng)(附源碼)
- Java?NIO實(shí)現(xiàn)聊天系統(tǒng)
- Java Socket實(shí)現(xiàn)多人聊天系統(tǒng)
- 基于Java網(wǎng)絡(luò)編程和多線程的多對多聊天系統(tǒng)
- Java網(wǎng)絡(luò)編程UDP實(shí)現(xiàn)多線程在線聊天
- Java網(wǎng)絡(luò)編程實(shí)例——簡單模擬在線聊天
- Java使用TCP實(shí)現(xiàn)在線聊天的示例代碼
- Java GUI編程實(shí)現(xiàn)在線聊天室
- Java中使用websocket實(shí)現(xiàn)在線聊天功能
- java實(shí)現(xiàn)在線聊天系統(tǒng)
相關(guān)文章
Servlet的兩種創(chuàng)建方式(xml?注解)示例詳解
這篇文章主要為大家介紹了Servlet的兩種創(chuàng)建方式(xml?注解)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08
淺談Java由于不當(dāng)?shù)膱?zhí)行順序?qū)е碌乃梨i
為了保證線程的安全,我們引入了加鎖機(jī)制,但是如果不加限制的使用加鎖,就有可能會導(dǎo)致順序死鎖(Lock-Ordering Deadlock)。本文將會討論一下順序死鎖的問題。2021-06-06
Opencv實(shí)現(xiàn)身份證OCR識別的示例詳解
這篇文章主要為大家詳細(xì)介紹了如何使用Opencv實(shí)現(xiàn)身份證OCR識別功能,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴可以跟隨小編一起了解一下2024-03-03
java操作mongodb時(shí),對象bean和DBObject相互轉(zhuǎn)換的方法(推薦)
下面小編就為大家?guī)硪黄猨ava操作mongodb時(shí),對象bean和DBObject相互轉(zhuǎn)換的方法(推薦)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-11-11

