实现了一个自定义应用层协议的socket传输(C-S模型)。具体要求如下图
Goal Get familiar with writing simple networking code using socketAPl.
Understand what is a protocol, and whatis itfor.
Annlicalinnlaverpontocol Expe Appicanon yer oroe o Experience how to
design application layer protocol. Get familiai with bit level
operations. Description In this assignment, you will design and
implement a service to evaluate arithmetic expressions, as well as a
testing client We use the simple client-server architecture, where a
dlient sends a server request with expressions to evaluate and a
server response back with results. You need to implement it using a
stream socket (based on TCP pool) Request what is 7+12-3? Client TCP
connection eval Server Response: itis 16 Protocol Spec Because the
service model provided by stream socketis a reliable stream of bytes
(no meaning atached to the data, and no boundary on messages). we need
to design an application layer protocol to define the syntax and
semantics of the data we send between client and server. Request
format Below is the speification for the request message. 1 Number of
expressions to evaluate. [2 bytes, encoded using network endianness]
Length of 1st expression in bytes. [2 bytes, encoded using network
endianness] String representation of 1st expression [sequence of
bytes] Length of 2nd expression in byte 2 bytes, encoded using network
endianness] i String reresentation of 2nd expression. [sequence of
bytes] Length of nth expression in bytes. [2 bytes, encoded using
network endianness]
8. String representation of nth expression. [sequence of bytes] Visualization of request format is shown in the picture below (note:
quotation is only for clarity). “3+12" “1+12-4“ 2 bytes 2 bytes 4
bytes 2 bytes 6 bytes Response format Below is the speification for
the response message. Number of answers. [2 bytes, encoded using
network endianess] Length of 1st answer in bytes. [12 bytes, encoded
using network endianness] String representation of 1st answer.
[sequence of bytes] Length of 2nd answer in bytes. [2 bytes, encoded
using network endianness] String represetation of 2nd answer.
[sequence of bytes] Length of nth answer in bytes. [2 bytes, encoded
using network endianess] String representation of nth answer.
[sequence of bytes] Visualization of response format is shown in the
picture below (note: quotation is only for clarity) 2 “15" 1 “g”
2bytes 2 bytes 2bytes 2 bytes 1 byte Requirements Stick with the
prolocol specification here, no mroifiation in any way.
2. Use stream socket API (ased on TCP protocol) eval server needs to be mlthreaded, and can handle requests concurrently. expression to
eval 日Al numbers in expressions are positive integers. b. eval server
is only required to handle +,’ (don’t need to worry about”" and T, no
T or 21 c. No white space in the exressonis.s 。 Assume all expressions
in requests are valid.
5. Implement a test client. The integers in the protocol must be encoded with big endian On Java implementation a Can only use
jva.io.InputStream to read from socket b. Can only use
java.io.OutputStream to write to socket C. If max bufer size is 16 if
using i.8 write(byte[] b,int off, int 1en)
11.3 read{byte[] b,int ofE。int 1en) Grading policy Assignment will be graded as per the fllowing rubric, Total points: 100 Points are
dstributed into 3 main categories and its further dstibutin is given
below. . Correctness of requestresponse message according to the
protocol: 30 points. 。Number of expressions field (with correct
endiannessk: 10 points 。Format of lengh of expressions (with corect
endianness) and the actual expressions: 20 lmplementation: 50 points
Handle concurent requests using threads: 20 points 。Use InputStream
and OuputStream to handle read and writ: 20 points 。Max buffer size no
more than 16: 10 points expression evaluation: 20 points
服务器端
package server;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
public class Calculation extends Thread{
private Socket socket;
public Calculation(Socket socket1){
this.socket=socket1;
}
@Override
public void run() {
try {
InputStream inputStream = socket.getInputStream();
OutputStream outputStream=socket.getOutputStream();
byte[] num =new byte[2];
inputStream.read(num);
outputStream.write(num);/
for(int i=0;i<((int)num[0])*10+(int)num[1];i++){//大端
byte[] length=new byte[2];
inputStream.read(length);
int intlength=((int)length[0])*10+(int)length[1];//
if(intlength>16){
System.out.println("TODO......");
}
else{
byte[] math=new byte[intlength];
inputStream.read(math);
byte c=0;
int result=0;
int flag=1;
int temp=0;
for(int j=0;j=0;j--){
byte m=(byte)(result%10);
result/=10;
resultbuff[j]=m;
}
outputStream.write(resultbuff);
}
}
inputStream.close();
outputStream.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
byte[] toenddian(int a){
byte[] by=new byte[2];
by[1]=(byte)(a&0xff);
by[0]=(byte)((a>>8)&0xff);
return by;
}
}
客户端
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
public class Client {
public void ask() throws IOException {
Socket s=new Socket("127.0.0.1",8010);
InputStream inputStream= s.getInputStream();
OutputStream outputStream=s.getOutputStream();
byte[] num=new byte[2];
byte[] b={0,2,0,5,2,3,'+',1,2,0,5,3,0,'-',2,0};
outputStream.write(b);
inputStream.read(num);
for(int i=0;i<((int)num[0])*10+(int)num[1];i++){
byte[] lenth=new byte[2];
inputStream.read(lenth);
for(int j=0;j<((int)lenth[0])*10+(int)lenth[1];j++){
byte[] bb=new byte[1];
inputStream.read(bb);
System.out.print(bb[0]);
}
}
s.close();
}
public static void main(String[] args) throws IOException {
Client client=new Client();
client.ask();
}
}



