我已创建套接字服务器和客户端类并成功执行。在为socket服务器和客户端创建JUnit测试类时,我得到了任何结果。 我猜测它正在等待客户端请求但是在创建客户端后它也没有执行。
的服务器类
PrimaryKeyValueStore.java
import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Collections;
import java.util.Set;
import college.Student;
public class PrimaryKeyValueStore {
private InMemoryCache inMemoryCache;
public PrimaryKeyValueStore() {
inMemoryCache=new InMemoryCache();
}
public void startServer(int port) {
ServerSocket serverSocket=null;
ObjectInputStream inputStreamFromClient=null;
Socket clientSocket=null;
try {
serverSocket=new ServerSocket(port);
while (true) {
clientSocket=serverSocket.accept();
inputStreamFromClient=new ObjectInputStream(clientSocket.getInputStream());
Student student=(Student) inputStreamFromClient.readObject();
switch(student.getRestMethod()) {
case "PUT": inMemoryCache.put(student.getUserName(),student);
new Thread(new PrimaryKeyValueStoreClient(clientSocket,Collections.EMPTY_SET)).start();
break;
case "GET": new Thread(new PrimaryKeyValueStoreClient(clientSocket,inMemoryCache.get(student.getUserName()))).start();
break;
case "DELETE": Set<Student> setStudent=inMemoryCache.get(student.getUserName());
inMemoryCache.delete(student.getUserName());
new Thread(new PrimaryKeyValueStoreClient(clientSocket,setStudent)).start();
break;
case "GETALL": new Thread(new PrimaryKeyValueStoreClient(clientSocket,inMemoryCache.getAll())).start();
break;
}
}
}
catch (Exception e) {
System.err.println("Server Error :: "+e.getMessage());
System.err.println("Localized :: "+e.getLocalizedMessage());
System.err.println("Stack Trace :: "+e.getStackTrace());
System.err.println("To String :: "+e.toString());
}finally {
if(inputStreamFromClient!=null) {
try {
inputStreamFromClient.close();
}catch(IOException e) {
e.printStackTrace();
}
}
if(serverSocket!=null) {
try {
serverSocket.close();
}catch(IOException e) {
e.printStackTrace();
}
}
}
}
}
,客户端类
CollegeClient.java
public class CollegeClient {
private static final String SOCKET_IP="127.0.0.1";
public Set<Student> sendMessage(Student student, int port) {
Socket clientSocket=null;
ObjectOutputStream outToServer=null;
ObjectInputStream fromServer=null;
try {
clientSocket=new Socket(SOCKET_IP, port);
outToServer=new ObjectOutputStream(clientSocket.getOutputStream());
outToServer.writeObject(student);
fromServer=new ObjectInputStream(clientSocket.getInputStream());
Set<Student> employeeSet=(Set<Student>) fromServer.readObject();
return employeeSet;
}catch(Exception e) {
System.err.println("Client Error :: "+e.getMessage());
System.err.println("Localized :: "+e.getLocalizedMessage());
System.err.println("Stack Trace :: "+e.getStackTrace());
}finally {
if(clientSocket!=null) {
try {
clientSocket.close();
}catch(IOException e) {
e.printStackTrace();
}
}
if(fromServer!=null) {
try {
fromServer.close();
}catch(IOException e) {
e.printStackTrace();
}
}
if(outToServer!=null) {
try {
outToServer.close();
}catch(IOException e) {
e.printStackTrace();
}
}
}
return null;
}
}
我的测试课是,
public class CollegeClientTest {
private CollegeClient collegeClient;
private PrimaryKeyValueStore primaryKeyValueStore;
@Before
public void setUp() {
collegeClient = new CollegeClient();
primaryKeyValueStore=new PrimaryKeyValueStore();
}
@Before
public void serverStart() {
primaryKeyValueStore.startServer(9000);
}
@Test
public void sendMessage() {
int port=9000;
Student student=new Student();
student.setUserName("a");
student.setFirstName("aa");
student.setLastName("aaa");
student.setAge(1);
student.setMark(100);
student.setRestMethod("GET");
Set<Student> employeeSet=collegeClient.sendMessage(student, port);
System.out.println("===>> "+employeeSet);
}
}
结果是,
任何人都可以帮助解决这个问题。