是这样的,我正在改写一个c在Solaris下写的socket程序,服务器端无法修改,只能自己摸索,改写后的java程序如下,但是运行的时候无论如何也无法取得返回值,因为in.ready()一直都是false,但是socket.isConnected()却一直都是true的。循环中等待了很久也无法取得返回结果。谢谢各位英雄相助,问题解决一定给分。public class myclass {

public static void main(String args[]){ 
String xltHost = "132.102.32.2";
int xltPort = 3001;
String message;
String s;
String line, tt;
String error;
//connect to xlt server
try{
Socket socket = new Socket(xltHost, xltPort);

PrintWriter out = new PrintWriter(socket.getOutputStream(), true);

BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

socket.setKeepAlive(true);

/*Send data over socket*/
s = "Login\r\nEND\r\n"; // 这是我要发送的数据
out.println(s); 

out.flush();

logFile("login send", s);

logFile("isConnected ?", Boolean.toString(socket.isConnected())); //这里得到的是TRUE。

tt = "";

//while ((line = in.readLine()) != null ) {
CharBuffer cb = CharBuffer.allocate(1024);
while(true){
logFile("isConnected ?", Boolean.toString(socket.isConnected())); //在循环中这里一直都是true,但是用系统命令netstat -n却看不到两台机器之间有建立连接,怎么回事?
logFile("ready 1", Boolean.toString(in.ready())); //在循环中这里一直都是false。

//line = in.readLine();
if (in.ready()){
in.read(cb.array(), 0, cb.length());
tt = cb.toString();

logFile("line", line);
if (line != null){
tt += line + "\r\n";
}
if (tt.indexOf("END") > 0){
logFile("end", "i got END");
break;
}
logFile("1", "3");
Thread.sleep(1000);
logFile("ready 2", Boolean.toString(in.ready()));
}
else{
Thread.sleep(1000);
continue;
}

}

if (tt.indexOf("Result=0") > 0){
logFile("login success: ", tt);
}
else{
logFile("login fail", tt);

logFile("end interface", "");
return;
}
//log out from xlt server

}
catch (IOException e){
e.printStackTrace();
error = e.toString();
System.out.println(error);
logFile("IOException: ", error);
System.exit(1);
}
catch (SecurityException e){
e.printStackTrace();
error = e.toString();
System.out.println(error);
logFile("SecurityException: ", error);
System.exit(1);
}
catch(Exception e){
e.printStackTrace();
error = e.toString();
System.out.println(error);
logFile("Exception catch:", error);
}

message = ">>>>end>>>>";
logFile("process end and quit", message);
logFile("", "");
} private static void logFile(String title, String msg)
{
try
{
FileOutputStream out = new FileOutputStream("log.txt",true);

out.write((new String("<")).getBytes());
out.write(title.getBytes());
out.write((new String(">")).getBytes());
out.write('\r');
out.write('\n');
out.write(msg.getBytes());
out.write('\r');
out.write('\n');

out.close();
}
catch (Exception e)
{
System.out.println(e.toString());
}
}
}

解决方案 »

  1.   

    高人快来啊,如果需要,我可以把原来的c程序发上来,很小很简单的一个socket程序,就是不知道怎么回事搞不定。看看我上面的java有没有技术上问题啊?谢谢。
      

  2.   

    这是整理过的代码。
    是这样的,我正在改写一个c在Solaris下写的socket程序,服务器端无法修改,只能自己摸索,改写后的java程序如下,但是运行的时候无论如何也无法取得返回值,因为in.ready()一直都是false,但是socket.isConnected()却一直都是true的。循环中等待了很久也无法取得返回结果。谢谢各位英雄相助,问题解决一定给分。public class myclass {

    public static void main(String args[]){  try{
    Socket socket = new Socket("132.102.32.2", 3001);

    PrintWriter out = new PrintWriter(socket.getOutputStream(), true);

    BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

    socket.setKeepAlive(true);

    /*Send data over socket*/
    s = "Login\r\nEND\r\n"; // 这是我要发送的数据
    out.println(s); 

    out.flush();

    logFile("isConnected ?", Boolean.toString(socket.isConnected())); //这里得到的是TRUE。 CharBuffer cb = CharBuffer.allocate(1024);
    while(true){
    logFile("isConnected ?", Boolean.toString(socket.isConnected())); //在循环中这里一直都是true,但是用系统命令netstat -n却看不到两台机器之间有建立连接,怎么回事?
    logFile("ready 1", Boolean.toString(in.ready())); //在循环中这里一直都是false。

    if (in.ready()){ //一直无法进入到这个判断里面
    in.read(cb.array(), 0, cb.length());
    tt = cb.toString();

    if (tt.indexOf("END") > 0){
    System.out.println("i got END");
    break;
    }
    Thread.sleep(1000);
    }
    else{
    Thread.sleep(1000);
    continue;
    }

    }

    }
    catch (IOException e){
    }
    catch (SecurityException e){
    }
    catch(Exception e){
    }

    }
      

  3.   

    下面是原来的C代码,其中包含了一些必要的访问数据库代码,由于实在对C不感冒,不容易剔除,请各位英雄见谅。#include <stdio.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <netdb.h>
    #include <errno.h>
    #include <signal.h>
    #include <string.h>#include "sybdb.h"#define SERV_TCP_PORT  3001
    #define MSG_SIZE        256LOGINREC *login;
    DBPROCESS *dbproc,*dbproc1;extern int errno;
    struct hostent *gethostbyname();
    struct hostent *hp;
    struct sockaddr_in peeraddr_in;void quitapp(int s)
    {
    if  (s >0 )
    close(s);
    dbexit();
    exit(ERREXIT);
    }void catcher(int a)
    {
    printf("Server interrupt caught\n");
    dbexit();
    exit(1);
    }void catcher1(int a)
    {
    printf("Server Broken Pipe caught\n");
    dbexit();
    exit(1);
    }main (int argc,char *argv[]) /* TCP client main */
    {
    int  s=-1,readchar;
    char outbuf[MSG_SIZE];
    char inbuf[1024],disbuf[512];
    char *ptr,rtnstr[13];
    int  on = 1;
    char username[20];
    char passwd[20];
    int  loca;
    int  timecount=0; DBCHAR szUserName[20];
    DBCHAR szPassword[20];
    DBCHAR szHostName[20];
    DBCHAR szAppName[20];
    DBCHAR szDatabase[20];
    DBINT  szserno;
    DBCHAR szcmd[201],rtncode[3],cmdstr[200];

    RETCODE result_code;
    RETCODE result_code1;

    if (argc < 2)
    {
    printf("Usage: program <remote hostname>\n");
    exit(1);
    }

    sigset(SIGINT,catcher);
    sigset(SIGPIPE,catcher1);

    if ((hp = gethostbyname("132.102.32.1")) == NULL)
    {
    printf("Host %%s not found\n", "132.102.32.1");
    quitapp(s);
    }

    memset((char *)&peeraddr_in, 0, sizeof(peeraddr_in));

    peeraddr_in.sin_family = AF_INET;
    memcpy((char *)&peeraddr_in.sin_addr, hp->h_addr, hp->h_length);
    peeraddr_in.sin_port = htons(SERV_TCP_PORT);
    //创建socket
    if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0)
    {
    perror("Unable to create socket");
    quitapp(s);
    }

    while (connect(s, (struct sockaddr *)&peeraddr_in, sizeof(peeraddr_in)) < 0)
    {
    if(errno != ECONNREFUSED)
    {
    perror("Connection attempt");
    printf("Connection attempt, errno = %d\n", errno);
    quitapp(s);
    }
    }

    if (setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, (char *)&on, sizeof(on)) < 0)
    {
    perror("Cannot set KEEPALIVE");
    quitapp(s);
    }

    if (dbinit() == FAIL)
    {
    printf("Error on dbinit()\n");
    quitapp(s);
    }

    sprintf(szUserName,"%s",getenv("USERNAME"));
    sprintf(szPassword,"%s",getenv("PASSWORD"));
    sprintf(szHostName,"%s",getenv("HOSTNAME"));
    sprintf(szAppName,"%s",getenv("APPNAME"));
    sprintf(szDatabase,"%s",getenv("DATABASENAME"));

    if ((login = dblogin()) == FAIL)
    {
    printf("Error on dblogin()\n");
    quitapp(s);
    }

    DBSETLUSER(login, szUserName);
    DBSETLPWD(login, szPassword);
    DBSETLHOST(login,szHostName);
    DBSETLAPP(login,szAppName);

    if ((dbproc = dbopen(login,NULL)) == NULL)
    {
    printf("Error on dbopen() dbproc\n");
    quitapp(s);
    }

    if ((dbproc1 = dbopen(login,NULL)) == NULL)
    {
    printf("Error on dbopen() dbproc1!\n");
    quitapp(s);
    }

    dbuse(dbproc,szDatabase);
    dbuse(dbproc1,szDatabase);

     /* test */
    if ((dbcmd(dbproc,"exec sp_pro1")) == FAIL)
    {
    perror("Error on dbcmd()\n");
    quitapp(s);
    }


    if ((dbsqlexec(dbproc)) == FAIL)
    {
    perror("Error on dbsqlexec()\n");
    quitapp(s);
    }

    memset(outbuf,0,sizeof(outbuf));
    sprintf(outbuf,"Login\r\nUserName=name\r\nPasswd=psw\r\nEND\r\n");
    write(s,outbuf,strlen(outbuf));


    if (strstr(inbuf,"Result=0") != NULL )
    {   
    printf("login fail \n");
    quitapp(s);
    }
     else 
    printf("*** login success! ***\n"); 

    while ((result_code = dbresults(dbproc)) != NO_MORE_RESULTS)
    {
    if (result_code == SUCCEED ) 
    {
    dbbind(dbproc,1,INTBIND,(DBINT)0,(BYTE *)&szserno);
    dbbind(dbproc,2,NTBSTRINGBIND,(DBINT)0,(BYTE DBFAR *)szcmd);

    while ((result_code=dbnextrow(dbproc)) != NO_MORE_ROWS)
    {
    memset(outbuf,0,sizeof(outbuf));
    sprintf(outbuf,"%s",szcmd);
    printf("%s\n",outbuf);  
    write(s,outbuf,strlen(outbuf));


    printf("%s\n",inbuf);
    sleep(1);

    if (strstr(inbuf,"PACKET=UP")) break;
    sleep(2);

         }

    if  ((strstr(inbuf,"ErrInfo=1") != NULL )||(strstr(inbuf,"ErrInfo=2") != NULL )) 
    sprintf(rtncode,"%s","00");
    else 
    sprintf(rtncode,"%s","01");
         
    sprintf(cmdstr,"%s %d,\"%s\"","exec sp_pro2",szserno,rtncode);

    dbcmd(dbproc1,cmdstr);
    dbsqlexec(dbproc1);
    while ((result_code1=dbresults(dbproc1)) != NO_MORE_RESULTS)
    {
    if (result_code == SUCCEED)
    {
    while ((result_code1=dbnextrow(dbproc1)) != NO_MORE_ROWS);
    }
    }
    memset(szcmd,0,sizeof(szcmd));
    }
    }
     
    while ((result_code1=dbresults(dbproc1)) != NO_MORE_RESULTS)
    {
    if (result_code == SUCCEED)
    {
    while ((result_code1=dbnextrow(dbproc1)) != NO_MORE_ROWS);
    }
    }

    memset(outbuf,0,sizeof(outbuf));
    sprintf(outbuf,"Logout\r\nEND\r\n");
    write(s,outbuf,strlen(outbuf));
    memset(inbuf,0,sizeof(inbuf));
    read(s,inbuf,sizeof(inbuf));
    loca = strstr(inbuf,"end") - inbuf;
    printf("%d\n",loca);
    printf("%s\n",inbuf);
    printf("*** Process end! ***\n\n\n");
     
    quitapp(s);   

    }