java socket send_java socket编程
TcpServer
Server端所要做的事情主要是建立一個(gè)通信的端點(diǎn),然后等待客戶端發(fā)送的請(qǐng)求。典型的處理步驟如下:
1. 構(gòu)建一個(gè)ServerSocket實(shí)例,指定本地的端口。這個(gè)socket就是用來監(jiān)聽指定端口的連接請(qǐng)求的。
2. 調(diào)用socket的accept()方法來獲得下面客戶端的連接請(qǐng)求。通過accept()方法返回的socket實(shí)例,建立了一個(gè)和客戶端的新連接。
3.通過這個(gè)返回的socket實(shí)例獲取InputStream和OutputStream,可以通過這兩個(gè)stream來分別讀和寫數(shù)據(jù)。
4.結(jié)束的時(shí)候調(diào)用socket實(shí)例的close()方法關(guān)閉socket連接。
packagesocket;importjava.io.IOException;importjava.io.InputStreamReader;importjava.io.Reader;importjava.net.ServerSocket;importjava.net.Socket;public classServer {public static void main(String args[]) throwsIOException {//為了簡(jiǎn)單起見,所有的異常信息都往外拋
int port = 8899;//定義一個(gè)ServerSocket監(jiān)聽在端口8899上
ServerSocket server = newServerSocket(port);//server嘗試接收其他Socket的連接請(qǐng)求,server的accept方法是阻塞式的
Socket socket =server.accept();//跟客戶端建立好連接之后,我們就可以獲取socket的InputStream,并從中讀取客戶端發(fā)過來的信息了。
Reader reader = newInputStreamReader(socket.getInputStream());char chars[] = new char[64];intlen;
StringBuilder sb= newStringBuilder();while ((len=reader.read(chars)) != -1) {
sb.append(new String(chars, 0, len));
}
System.out.println("from client: " +sb);
reader.close();
socket.close();
server.close();
}
}
TcpClient端
客戶端的請(qǐng)求過程稍微有點(diǎn)不一樣:
1.構(gòu)建Socket實(shí)例,通過指定的遠(yuǎn)程服務(wù)器地址和端口來建立連接。
2.通過Socket實(shí)例包含的InputStream和OutputStream來進(jìn)行數(shù)據(jù)的讀寫。
3.操作結(jié)束后調(diào)用socket實(shí)例的close方法,關(guān)閉。
packagesocket;importjava.io.OutputStreamWriter;importjava.io.Writer;importjava.net.Socket;public classClient {public static void main(String args[]) throwsException {//為了簡(jiǎn)單起見,所有的異常都直接往外拋
String host = "10.21.23.144"; //要連接的服務(wù)端IP地址
int port = 8899; //要連接的服務(wù)端對(duì)應(yīng)的監(jiān)聽端口//與服務(wù)端建立連接
Socket client = newSocket(host, port);//建立連接后就可以往服務(wù)端寫數(shù)據(jù)了
Writer writer = newOutputStreamWriter(client.getOutputStream());
writer.write("Hello mynona Server.");
writer.flush();//寫完后要記得flush
writer.close();
client.close();
}
}
Udp:
server端
因?yàn)閁DP協(xié)議不需要建立連接,它的過程如下:
1. 構(gòu)造DatagramSocket實(shí)例,指定本地端口。
2. 通過DatagramSocket實(shí)例的receive方法接收DatagramPacket.DatagramPacket中間就包含了通信的內(nèi)容。
3. 通過DatagramSocket的send和receive方法來收和發(fā)DatagramPacket.
packageUdp2;importjava.net.DatagramPacket;importjava.net.DatagramSocket;importjava.net.InetAddress;public classUDPServer {public static void main(String args[]) throwsException
{//監(jiān)聽9876端口
DatagramSocket serverSocket = new DatagramSocket(9876);byte[] receiveData = new byte[1024];byte[] sendData = new byte[1024];while(true)
{//構(gòu)造數(shù)據(jù)包接收數(shù)據(jù)
DatagramPacket receivePacket = newDatagramPacket(receiveData, receiveData.length);//接收數(shù)據(jù)
serverSocket.receive(receivePacket);//解析數(shù)據(jù)
String sentence = newString( receivePacket.getData());
System.out.println("RECEIVED: " +sentence);
InetAddress IPAddress=receivePacket.getAddress();int port =receivePacket.getPort();
String capitalizedSentence=sentence.toUpperCase();
sendData=capitalizedSentence.getBytes();//構(gòu)造發(fā)送數(shù)據(jù)包發(fā)送數(shù)據(jù)
DatagramPacket sendPacket = newDatagramPacket(sendData, sendData.length, IPAddress, port);
serverSocket.send(sendPacket);
}
}
}
UdpClient:
1. 構(gòu)造DatagramSocket實(shí)例。
2.通過DatagramSocket實(shí)例的send和receive方法發(fā)送DatagramPacket報(bào)文。
3.結(jié)束后,調(diào)用DatagramSocket的close方法關(guān)閉。
packageUdp2;importjava.io.BufferedReader;importjava.io.InputStreamReader;importjava.net.DatagramPacket;importjava.net.DatagramSocket;importjava.net.InetAddress;public classUDPClient {public static void main(String args[]) throwsException
{
BufferedReader inFromUser=
new BufferedReader(newInputStreamReader(System.in));
DatagramSocket clientSocket= newDatagramSocket();
InetAddress IPAddress= InetAddress.getByName("localhost");
System.out.println("IPAddress:" +IPAddress);byte[] sendData = new byte[1024];byte[] receiveData = new byte[1024];
String sentence=inFromUser.readLine();
sendData=sentence.getBytes();//發(fā)送數(shù)據(jù)
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 9876);
clientSocket.send(sendPacket);//接收數(shù)據(jù)
DatagramPacket receivePacket = newDatagramPacket(receiveData, receiveData.length);
clientSocket.receive(receivePacket);
String modifiedSentence= newString(receivePacket.getData());
System.out.println("FROM SERVER:" +modifiedSentence);
clientSocket.close();
}
}
參考來源:http://shmilyaw-hotmail-com.iteye.com/blog/1556187
http://haohaoxuexi.iteye.com/blog/1979837
總結(jié)
以上是生活随笔為你收集整理的java socket send_java socket编程的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c语言的使用比例远远高于python语言
- 下一篇: 怎么将电脑文件夹图标换成自己喜欢的图片怎