我写了 一个java的socket通信的类,想要封装成一个.DLL文件(客户端),不知道怎么实现,客户端用c++实现的最好,请教高手解决。
我实现的socketClient代码:public class SocketClient {

private Socket socket;
private BufferedReader in;
private PrintWriter out;

/**
 * 
 */
public AllocTimeStampClient() {
try {
System.out.println("Try to connect to 127.0.0.1:3600");
//socket = new Socket("127.0.0.1",80);//直接写定Server端的IP
socket = new Socket(InetAddress.getLocalHost(),3600);
System.out.println("The Server Connected!");
System.out.println("Please enter some Character:");
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(),true);
BufferedReader wt  = new BufferedReader(new InputStreamReader(System.in));
while(true){
String str = wt.readLine();
System.out.println(str);
out.println(str);
out.flush();
if(str.equals("end")||str.equals("END")) {
break;
}
}
}catch(IOException e) {
e.printStackTrace();
}finally {
out.close();
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
new AllocTimeStampClient();
}
}
怎么样才能实现一个.DLL的这个功能呀,急!!!,等待在线解答,请详细给出,谢谢。

解决方案 »

  1.   

    dll文件要用c++生成,具体的你可以从网上down一个c写的socket的例子,编译成dll文件
    java调用dll需要使用jni,这个也可以从网上down个例子然后改一下,用过挺麻烦的
      

  2.   

    你java写的这个socket sever如果是与c++写的socket client连接可能会有问题。我原来就发现过这种错误,看我的帖子http://community.csdn.net/Expert/topic/5446/5446650.xml?temp=.6735651
      

  3.   

    你再仔细看看,你这个帖子上贴的源码与我原来错误的那个例子一样,和我之后修改的那个是有区别的。我原来也发生过你这种情况,发出去后服务器端卡在那里,client端收不到返回的信息。
      

  4.   

    class TCPClient {    public static void main(String args[]) {        String data;
            Socket client;
            DataInputStream inputS;        OutputStream outputS;        int i = 0;
            try {
                client = new Socket("127.0.0.1", 600);
                inputS = new DataInputStream(client.getInputStream());
                outputS = client.getOutputStream();
            }
            catch (IOException e) {
                System.out.println("Cannot Connect with Server");
                return;
            }        try {
                Encoder encoder = new Encoder(500);            encoder.putInt32(0x01);
                encoder.putInt32(0x00);
                encoder.putInt32(0x00);
                encoder.putInt32(0x00);            String name = "herod";
                String password = "password";
                encoder.putString(name);
                encoder.putString(password);
                System.out.println("encoder.getBytes().length is:" +
                                   encoder.getBytes().length);
                encoder.setLengthField32(encoder.getBytes().length - 5);
                byte[] bytes = encoder.getBytes();
                System.out.println("bytes is:" + bytes);            //data = KeyS.readLine();
                outputS.write(bytes);
                outputS.flush();
                System.out.println("before from server");            Thread.sleep(3000);
                int l = inputS.available();
                if (l > 0) {
                    bytes = new byte[l];
                    System.out.println("l is:" + l);
                    while (i < l) {
                        bytes[i++] = inputS.readByte();
                    }
                    Decoder decoder = new Decoder(bytes);                System.out.println("the first number is:" +
                                       decoder.getInt32());
                    System.out.println("the second number is:" +
                                       decoder.getInt32());
                    System.out.println("the third number is:" +
                                       decoder.getInt32());
                    System.out.println("the forth number is:" +
                                       decoder.getInt32());
                    System.out.println("the five number is:" + decoder.getInt32());
                    System.out.println("the first String is:" +
                                       decoder.getString());
                    System.out.println("the second String is:" +
                                       decoder.getString());
                    System.out.println("ok in here");
                }        }
            catch (Exception e) {
                System.out.println("IOException  Happened:" + e);
            }        try {
                System.out.println("Now will end this program");
                client.close();
                return;        }
            catch (IOException e) {
                System.out.println("system cannot close socket");
            }    }}
    这个是我改过之后的JAVA (client),对之前的一个改动,现在用c++实现要通过一定的格式,我不知道该怎么实现,就是我要在服务器端验证客户端发来的包头,如果错误,就返回错误消息,否则就进行处理,中间就是用了一个类来对发送的客户端的内容和包头信息一起组成一个包信息一起发送给服务器,验证格式——》处理——》返回——》客户端信息。
    用c++怎么实现呀。以下有我的代码
    // ClientSocket.cpp : implementation file
    //#include "stdafx.h"
    #include "tcpclient.h"
    #include "ClientSocket.h"
    #include "TcpclientDlg.h"#ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif/////////////////////////////////////////////////////////////////////////////
    // CClientSocketCClientSocket::CClientSocket()
    {
    }CClientSocket::~CClientSocket()
    {
    }
    // Do not edit the following lines, which are needed by ClassWizard.
    #if 0
    BEGIN_MESSAGE_MAP(CClientSocket, CSocket)
    //{{AFX_MSG_MAP(CClientSocket)
    //}}AFX_MSG_MAP
    END_MESSAGE_MAP()
    #endif // 0/////////////////////////////////////////////////////////////////////////////
    // CClientSocket member functionsvoid CClientSocket::OnReceive(int nErrorCode) 
    {
    char chr[256];
    memset(chr,0,256); this->Receive(chr,255); ((CTcpclientDlg*)AfxGetMainWnd())->m_list.AddString(chr);
    ((CTcpclientDlg*)AfxGetMainWnd())->m_list.SendMessage(WM_VSCROLL,SB_BOTTOM); CSocket::OnReceive(nErrorCode);
    }
    // tcpclient.cpp : Defines the class behaviors for the application.
    //#include "stdafx.h"
    #include "tcpclient.h"
    #include "tcpclientDlg.h"#ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif/////////////////////////////////////////////////////////////////////////////
    // CTcpclientAppBEGIN_MESSAGE_MAP(CTcpclientApp, CWinApp)
    //{{AFX_MSG_MAP(CTcpclientApp)
    // NOTE - the ClassWizard will add and remove mapping macros here.
    //    DO NOT EDIT what you see in these blocks of generated code!
    //}}AFX_MSG
    ON_COMMAND(ID_HELP, CWinApp::OnHelp)
    END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
    // CTcpclientApp constructionCTcpclientApp::CTcpclientApp()
    {
    // TODO: add construction code here,
    // Place all significant initialization in InitInstance
    }/////////////////////////////////////////////////////////////////////////////
    // The one and only CTcpclientApp objectCTcpclientApp theApp;/////////////////////////////////////////////////////////////////////////////
    // CTcpclientApp initializationBOOL CTcpclientApp::InitInstance()
    {
    AfxEnableControlContainer();
    AfxSocketInit();
    // Standard initialization
    // If you are not using these features and wish to reduce the size
    //  of your final executable, you should remove from the following
    //  the specific initialization routines you do not need.#ifdef _AFXDLL
    Enable3dControls(); // Call this when using MFC in a shared DLL
    #else
    Enable3dControlsStatic(); // Call this when linking to MFC statically
    #endif CTcpclientDlg dlg;
    m_pMainWnd = &dlg;
    int nResponse = dlg.DoModal();
    if (nResponse == IDOK)
    {
    // TODO: Place code here to handle when the dialog is
    //  dismissed with OK
    }
    else if (nResponse == IDCANCEL)
    {
    // TODO: Place code here to handle when the dialog is
    //  dismissed with Cancel
    } // Since the dialog has been closed, return FALSE so that we exit the
    //  application, rather than start the application's message pump.
    return FALSE;
    }
      

  5.   

    是这样的, 现在是不要求用户自己发送,而是提供一个DLL客户端,要求别的应用直接调用DLL,之后客户端和服务器建立连接,发送——》处理——》返回,是自动执行的,我不知道该怎么实现DLL,请求给点解答实现DLL通信!!!在线等待