【问题标题】:How can I implement a c++ socket server against a java client?如何针对 Java 客户端实现 C++ 套接字服务器?
【发布时间】:2012-02-06 15:49:38
【问题描述】:

我正在开发一个客户端-服务器应用程序。客户端是基于 Java 的,服务​​器端是 Windows 中的 C++。

我正在尝试使用 Socket 与它们通信,但遇到了一些麻烦。

我已经成功地将客户端与 Java 服务器通信,以测试是否是我的客户端出错了,但不是,我似乎在 C++ 版本中做得不对。

java 服务器是这样的:

    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.io.IOException;
    import java.net.ServerSocket;
    import java.net.Socket;


    public class Server {

     public static void main(String[] args){
         boolean again = true;
         String mens;
      ServerSocket serverSocket = null;
      Socket socket = null;
      DataInputStream dataInputStream = null;
      DataOutputStream dataOutputStream = null;

      try {
       serverSocket = new ServerSocket(12321);
       System.out.println("Listening :12321");
      } catch (IOException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
      }

      while(again){
       try {
           System.out.println("Waiting connection...");
        socket = serverSocket.accept();
        System.out.println("Connected");
        dataInputStream = new DataInputStream(socket.getInputStream());
        dataOutputStream = new DataOutputStream(socket.getOutputStream());
        while (again){
            mens = dataInputStream.readUTF();
            System.out.println("MSG: " + mens);
            if (mens.compareTo("Finish")==0){
                again = false;
            }
        }
       } catch (IOException e) {
           System.out.println("End of connection");
        //e.printStackTrace();
       }
       finally{
        if( socket!= null){
         try {
          socket.close();
         } catch (IOException e) {
          // TODO Auto-generated catch block
          //e.printStackTrace();
         }
        }

        if( dataInputStream!= null){
         try {
          dataInputStream.close();
         } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
         }
        }

        if( dataOutputStream!= null){
         try {
          dataOutputStream.close();
         } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
         }
        }
       }
      }
      System.out.println("End of program");
     }
    }

客户端只是建立一个连接并发送一些用户介绍的消息。

您能否给我一个类似的工作服务器,但使用 C++(在 Windows 中)? 我自己做不出来。

谢谢。

【问题讨论】:

  • 粘贴客户端代码并从服务器端日志中跟踪。
  • 此外,一个简单的 Google 搜索将显示数百甚至数千个在 Windows 中制作套接字服务器的示例和教程。

标签: java c++ sockets client-server


【解决方案1】:

您的问题是您发送的 java 字符串每个字符可能占用 1 或 2 个字节(请参阅 bytes of a string in java?

您需要以 ascii 字节发送和接收以使事情变得更容易,想象data 是您在客户端的数据字符串:

byte[] dataBytes = data.getBytes(Charset.forName("ASCII")); 

for (int lc=0;lc < dataBytes.length ; lc++)
{
    os.writeByte(dataBytes[lc]);
}

byte responseByte = 0;
char response = 0;

responseByte = is.readByte();
response  = (char)responseByte;

其中isos 分别是客户端DataInputStreamDataOutputStream

您还可以嗅探您的 tcp 流量以查看发生了什么:)

【讨论】:

  • 我会看看这个,thanx :)
猜你喜欢
  • 2015-09-16
  • 2012-06-24
  • 1970-01-01
  • 2012-06-05
  • 2012-11-15
  • 2017-05-26
  • 2014-03-10
  • 2017-02-25
  • 1970-01-01
相关资源
最近更新 更多