我在这里有一项任务,我无法完成任务,也无法确定哪种技术可以选择我的问题。这是我的问题的解释。我的应用程序是独立应用程序,将部署在不同的位置。让我们说LocA,LocB等。每个位置都有自己的客户端,一些客户端通过LAN连接,一些客户端是第三方客户端。只要客户端通过LAN连接就没有请求和响应的问题。但是很少有客户端没有通过局域网连接。
对于第三方客户,我们将提供IP和PORT,以标准HL7格式连接和请求数据。这里的问题是客户可以使用任何技术创建自己的客户端程序。他们可能使用Socket编程,.NET套接字编程等。但是我们的服务器应该接受来自客户端的连接,无论技术是什么,处理他们的请求并回复它们。并且要求服务器程序应该始终运行并监听分配的端口,或者只要有特定端口的请求,然后启动我们的服务器程序并处理客户端请求并回复客户端。
如果我的英语让你困惑,请原谅我。请给我一个解决我问题的方法。
注意:客户端程序可以是任何技术和任何编程语言。这取决于客户端他们想用来连接服务器并发送请求的方式。
答案 0 :(得分:1)
使用TCP套接字连接以及在客户端和服务器之间传递的序列化对象。这是必需的,否则转移的信息将在翻译中丢失。使用此方法,您必须向客户端提供要求(序列化对象),以便程序可以相互通信。如果您使用(来自java)套接字连接,它将能够接受端口上的任何传入TCP连接。
以下是我所指的
的好主意try
{
socket = new ServerSocket(portnum);
}
catch (IOException ioe)
{
output.append("IOException getting server socket\n" + ioe + "\n");
return;
}
while (true)
{
try
{
client = socket.accept();
output.append("Connected to client at " + client.getInetAddress());
clientHandler(client);
}
catch (IOException ioe)
{
output.append("IOException during accept \n " + ioe + "\n");
return;
}
}
这将在端口“portnum”上接受任何TCP连接请求。
确保多线程化您的应用程序,否则在建立连接时您将无法接受任何连接请求。
答案 1 :(得分:1)
我在这里给出一个小例子,客户端和服务器如何与Socket一起工作,我还在服务器端包含多线程。
客户端代码:
public class ClientWala {
public static void main(String[] args) throws Exception{
Boolean b = true;
Socket s = new Socket("127.0.0.1", 4444);
System.out.println("connected: "+s.isConnected());
OutputStream output = s.getOutputStream();
PrintWriter pw = new PrintWriter(output,true);
// to write data to server
while(b){
if (!b){
System.exit(0);
}
else {
pw.write(new Scanner(System.in).nextLine());
}
}
// to read data from server
InputStream input = s.getInputStream();
InputStreamReader isr = new InputStreamReader(input);
BufferedReader br = new BufferedReader(isr);
String data = null;
while ((data = br.readLine())!=null){
// Print it using sysout, or do whatever you want with the incoming data from server
}
}
}
服务器端代码:
import java.io.*
import java.net.*;
public class ServerTest {
ServerSocket s;
public void go() {
try {
s = new ServerSocket(44457);
while (true) {
Socket incoming = s.accept();
Thread t = new Thread(new MyCon(incoming));
t.start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
class MyCon implements Runnable {
Socket incoming;
public MyCon(Socket incoming) {
this.incoming = incoming;
}
@Override
public void run() {
try {
PrintWriter pw = new PrintWriter(incoming.getOutputStream(),
true);
InputStreamReader isr = new InputStreamReader(
incoming.getInputStream());
BufferedReader br = new BufferedReader(isr);
String inp = null;
boolean isDone = true;
System.out.println("TYPE : BYE");
System.out.println();
while (isDone && ((inp = br.readLine()) != null)) {
System.out.println(inp);
if (inp.trim().equals("BYE")) {
System.out
.println("THANKS FOR CONNECTING...Bye for now");
isDone = false;
s.close();
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
try {
s.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
e.printStackTrace();
}
}
}
public static void main(String[] args) {
new ServerTest().go();
}
}
答案 2 :(得分:0)
看到你提到的HL7,你应该看看HAPI (http://hl7api.sourceforge.net/) 它附带了大量的文档,包括this example用于发送&接收消息。
请注意,只要HL7的实现兼容,客户端程序使用的技术无关紧要。另外:套接字在没有LAN的情况下工作正常,因为127.0.0.1(本地主机地址)始终可用。