以下是一個(gè)展示java使用tcp通訊的簡(jiǎn)單例子,包括服務(wù)器和客戶端代碼:
我們提供的服務(wù)有:成都網(wǎng)站制作、網(wǎng)站設(shè)計(jì)、微信公眾號(hào)開(kāi)發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、廣陵ssl等。為成百上千家企事業(yè)單位解決了網(wǎng)站和推廣的問(wèn)題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的廣陵網(wǎng)站制作公司
/**
*TCPServer
*/
import java.io.*;
import java.net.*;
class TCPServer{
public static void main(String[] args)throws IOException{
ServerSocket listen = new ServerSocket(5050);
Socket server = listen.accept();
InputStream in = server.getInputStream();
OutputStream out = server.getOutputStream();
char c = (char)in.read();
System.out.println("收到:" + c);
out.write('s');
out.close();
in.close();
server.close();
listen.close();
}
}
/**
*TCPClient
*/
import java.io.*;
import java.net.*;
class TCPClient{
public static void main(String[] args)throws IOException{
Socket client = new Socket("127.0.0.1" , 5050);
InputStream in = client.getInputStream();
OutputStream out = client.getOutputStream();
out.write('c');
char c = (char)in.read();
System.out.println("收到:" + c);
out.close();
in.close();
client.close();
}
}
1、服務(wù)器端
import?java.io.DataOutputStream;
import?java.io.IOException;
import?java.net.ServerSocket;
import?java.net.Socket;
public?class?SocketServer?{
private?static?final?int?PORT?=?8088;
public?static?void?main(String[]?args)?{
ServerSocket?server?=?null;
try?{
server?=?new?ServerSocket(PORT);
while?(true)?{
Socket?client?=?server.accept();
new?Thread(new?Server(client)).start();
}
}?catch?(IOException?e)?{
e.printStackTrace();
}
}
}
class?Server?implements?Runnable?{
private?Socket?client;
public?Server(Socket?client)?{
this.client?=?client;
}
public?void?run()?{
DataOutputStream?output?=?null;
try?{
output?=?new?DataOutputStream(client.getOutputStream());
output.writeUTF("你好我是服務(wù)器");
}?catch?(IOException?e)?{
e.printStackTrace();
}?finally?{
try?{
if?(output?!=?null)?output.close();
output?=?null;
}?catch?(IOException?e)?{}
}
}
}
2、客戶端
import?java.io.DataInputStream;
import?java.io.IOException;
import?java.net.Socket;
import?java.net.UnknownHostException;
public?class?Client?extends?Socket?{
private?static?final?int?PORT?=?8088;
public?static?void?main(String[]?args)?{
Socket?socket?=?null;
try?{
socket?=?new?Socket("127.0.0.1",?PORT);
DataInputStream?in?=?new?DataInputStream(socket.getInputStream());
String?res?=?in.readUTF();
System.out.println(res);
if?(in?!=?null)?in.close();
}?catch?(UnknownHostException?e)?{
e.printStackTrace();
}?catch?(IOException?e)?{
e.printStackTrace();
}?finally?{
if?(socket?!=?null)?{
try?{
socket.close();
}?catch?(IOException?e)?{}
}
}
}
}
package?com.weixin.test;
import?java.io.IOException;
import?java.io.InputStream;
import?java.io.OutputStream;
import?java.net.InetAddress;
import?java.net.ServerSocket;
import?java.net.Socket;
import?org.junit.Test;
public?class?ScoketTest?{
@Test
public?void?client()?throws?Exception{
InetAddress?i=InetAddress.getByName("127.0.0.1");
Socket?s=new?Socket(i,?9000);
OutputStream?outputStream?=?s.getOutputStream();
outputStream.write("服務(wù)端你好,我是客戶端哦!".getBytes());
s.shutdownOutput();
InputStream?inputStream=s.getInputStream();
int?length=0;
byte[]?bytes=new?byte[1024];
while?((length=inputStream.read(bytes))!=-1)?{
System.err.println(new?String(bytes,0,length));
}
inputStream.close();
outputStream.close();
s.close();
}
@Test
public?void?server()?throws?Exception{
ServerSocket?serverSocket=new?ServerSocket(9000);
Socket?socket?=?serverSocket.accept();
InputStream?inputStream?=?socket.getInputStream();
OutputStream?outputStream?=?socket.getOutputStream();
int?length=0;
byte[]?bytes=new?byte[1024];
while?((length=inputStream.read(bytes))!=-1)?{
System.err.println(new?String(bytes,?0,length));
}
outputStream.write("客戶端你好,本王已收到!".getBytes());
outputStream.close();
inputStream.close();
socket.close();
serverSocket.close();
}
}
import java.net.ServerSocket;
import java.net.Socket;
public class TcpServer
{
public static void main(String[] args) throws Exception
{
// 創(chuàng)建服務(wù)器端的socket對(duì)象
ServerSocket ss = new ServerSocket(5000);
// 監(jiān)聽(tīng)連接
Socket socket = ss.accept();
// 直到連接建立好之后代碼才會(huì)往下執(zhí)行
System.out.println("Connected Successfully!");
}
}
import java.net.Socket;
public class TcpClient
{
public static void main(String[] args) throws Exception
{
Socket socket = new Socket("127.0.0.1", 5000);
}
}
localhost是指你本機(jī),它可以改成127.0.0.1,如果跟其他機(jī)器通訊就改成另一臺(tái)機(jī)器的ip地址。8800是指通訊端口;
你把OS里的O理解為output,IS里的I理解為input。OS是輸出端,輸出端把數(shù)據(jù)放到BufferedReader,BufferedReader通過(guò)端口傳輸?shù)絀S后,再讀取。
1.TCP/IP協(xié)議要求信息必須在塊(chunk)中發(fā)送和接收,而塊的長(zhǎng)度必須是8位的倍數(shù),因此,我們可以認(rèn)為T(mén)CP/IP協(xié)議中傳輸?shù)男畔⑹亲止?jié)序列。如何發(fā)送和解析信息需要一定的應(yīng)用程序協(xié)議。
2.信息編碼:
首先是Java里對(duì)基本整型的處理,發(fā)送時(shí),要注意:1)每種數(shù)據(jù)類型的字節(jié)個(gè)數(shù);2)這些字節(jié)的發(fā)送順序是怎樣的?(little-endian還是
big-endian);3)所傳輸?shù)臄?shù)值是有符號(hào)的(signed)還是無(wú)符號(hào)的(unsigned)。具體編碼時(shí)采用位操作(移位和屏蔽)就可以了。
具體在Java里,可以采用DataOutputStream類和ByteArrayOutputStream來(lái)實(shí)現(xiàn)?;謴?fù)時(shí)可以采用
DataInputStream類和ByteArrayInputStream類。
其次,字符串和文本,在一組符號(hào)與一組整數(shù)之間的映射稱為編碼字符集(coded character
set)。發(fā)送者與接收者必須在符號(hào)與整數(shù)的映射方式上達(dá)成共識(shí),才能使用文本信息進(jìn)行通信,最簡(jiǎn)單的方法就是定義一個(gè)標(biāo)準(zhǔn)字符集。具體編碼時(shí)采用
String的getBytes()方法。
最后,位操作。如果設(shè)置一個(gè)特定的設(shè)為1,先設(shè)置好掩碼(mask),之后用或操作;要清空特定一位,用與操作。
3.成幀與解析
成幀(framing)技術(shù)解決了接收端如何定位消息的首位位置的問(wèn)題。
如果接收者試圖從套接字中讀取比消息本身更多的字節(jié),將可能發(fā)生以下兩種情況之一:如果信道中沒(méi)有其他消息,接收者將阻塞等待,同時(shí)無(wú)法處理接收
到的消息;如果發(fā)送者也在等待接收端的響應(yīng)消息,則會(huì)形成死鎖(dealock);另一方面,如果信道中還有其他消息,則接收者會(huì)將后面消息的一部分甚至
全部讀到第一條消息中去,這將產(chǎn)生一些協(xié)議錯(cuò)誤。因此,在使用TCP套接字時(shí),成幀就是一個(gè)非常重要的考慮因素。
有兩個(gè)技術(shù):
1.基于定界符(Delimiter-based):消息的結(jié)束由一個(gè)唯一的標(biāo)記(unique
marker)指出,即發(fā)送者在傳輸完數(shù)據(jù)后顯式添加的一個(gè)特殊字節(jié)序列。這個(gè)特殊標(biāo)記不能在傳輸?shù)臄?shù)據(jù)中出現(xiàn)。幸運(yùn)的是,填充(stuffing)技術(shù)
能夠?qū)ο⒅谐霈F(xiàn)的定界符進(jìn)行修改,從而使接收者不將其識(shí)別為定界符。在接收者掃描定界符時(shí),還能識(shí)別出修改過(guò)的數(shù)據(jù),并在輸出消息中對(duì)其進(jìn)行還原,從而
使其與原始消息一致。
2.顯式長(zhǎng)度(Explicit length):在變長(zhǎng)字段或消息前附加一個(gè)固定大小的字段,用來(lái)指示該字段或消息中包含了多少字節(jié)。這種方法要確定消息長(zhǎng)度的上限,以確定保存這個(gè)長(zhǎng)度需要的字節(jié)數(shù)。
接口:
Java代碼 import java.io.IOException; import java.io.OutputStream; public interface Framer { void frameMsg(byte [] message,OutputStream out) throws IOException; byte [] nextMsg() throws IOException; }
定界符的方式:
Java代碼 import java.io.ByteArrayOutputStream; import java.io.EOFException; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; public class DelimFramer implements Framer { private InputStream in;//data source; private static final byte DELIMTER=(byte)'\n';//message delimiter public DelimFramer(InputStream in){ this.in=in; } @Override public void frameMsg(byte[] message, OutputStream out) throws IOException { //ensure that the message dose not contain the delimiter for(byte b:message){ if(b==DELIMTER) throw new IOException("Message contains delimiter"); } out.write(message); out.write(DELIMTER); out.flush(); } @Override public byte[] nextMsg() throws IOException { ByteArrayOutputStream messageBuffer=new ByteArrayOutputStream(); int nextByte; while((nextByte=in.read())!=DELIMTER){ if(nextByte==-1){//end of stream? if(messageBuffer.size()==0){ return null; }else{ throw new EOFException("Non-empty message without delimiter"); } } messageBuffer.write(nextByte); } return messageBuffer.toByteArray(); } }
顯式長(zhǎng)度方法:
Java代碼 import java.io.DataInputStream; import java.io.EOFException; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; public class LengthFramer implements Framer { public static final int MAXMESSAGELENGTH=65535; public static final int BYTEMASK=0xff; public static final int SHOTMASK=0xffff; public static final int BYTESHIFT=8; private DataInputStream in;// wrapper for data I/O public LengthFramer(InputStream in) throws IOException{ this.in=new DataInputStream(in); } @Override public void frameMsg(byte[] message, OutputStream out) throws IOException { if(message.lengthMAXMESSAGELENGTH){ throw new IOException("message too long"); } //write length prefix out.write((message.lengthBYTEMASK)BYTEMASK); out.write(message.lengthBYTEMASK); //write message out.write(message); out.flush(); } @Override public byte[] nextMsg() throws IOException { int length; try{ length=in.readUnsignedShort(); }catch(EOFException e){ //no (or 1 byte) message; return null; } //0=length=65535; byte [] msg=new byte[length]; in.readFully(msg);//if exception,it's a framing error; return msg; } }