J2ME中文传递问题

来源:互联网 发布:英语听力题目软件 编辑:程序博客网 时间:2024/06/14 16:52
环境:客户端: win98、 jdk1.3.1、 j2me wireless toolkit 1.0.1
      服务器端:Linux7.1 有中文支持
目的:服务器端c程序接收客户端java程序传递来的中文并显示出来;
说明:当客户端用纯java应用程序、服务器端c程序不变能正常显示中文。 
      
服务器端c程序:
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <sys/time.h>
#include <string.h>
#include <signal.h>
#include <stdio.h>



#define BUFLEN 4 /*缓冲区尺寸*/
#define SOCKADDR struct sockaddr
#define PORT 9696   /*服务器使用的端口号 */ 

int main(void)           /*主函数*/
{
int sockfd,socklen,newfd;
char buffer[BUFLEN];

struct sockaddr_in srv_addr,cli_addr;

fd_set infds;
int maxfd,n;
struct timeval tval;

    if ((sockfd=socket(AF_INET,SOCK_STREAM,0))<0)
{
printf("socket error!\n");
exit(1);
}
memset(&srv_addr,0,sizeof(struct sockaddr_in));
srv_addr.sin_family=AF_INET;
srv_addr.sin_port=htons(PORT);
srv_addr.sin_addr.s_addr=htonl(INADDR_ANY);
if(bind(sockfd,(SOCKADDR *)(&srv_addr),sizeof(struct sockaddr_in))<0)
{      /*绑定*/
printf("bind error!\n");
close(sockfd);
exit(2);
}
listen(sockfd,MAX);
socklen=sizeof(SOCKADDR);
    newfd=accept(sockfd,(SOCKADDR *)(&cli_addr),&socklen);
printf("accept success\n");
while((n=read(newfd,buffer,BUFLEN))>0)  /*读取客户发送来的信息*/
{
printf("buffer=%s\n",buffer);

}

close(newfd);

}

客户端java程序:

package Test;

import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import javax.microedition.io.*;
import java.io.*;

public class test1 extends MIDlet implements CommandListener {

    
static final String temp="你好";
private String serverUrl = "socket://61.188.177.33:4445";
StreamConnection c= null;
    DataOutputStream output;
    public test1() {
        
    }

    
    public void startApp() {
    try{
      System.out.println("begin");
      c=(StreamConnection)Connector.open(serverUrl);
      System.out.println("Created Socket\n");
      output=new DataOutputStream(c.openDataOutputStream());
      System.out.println("Created output stream");
      byte bufout[]=new byte[40];
      bufout=temp.getBytes() ;
      output.write(bufout,0,bufout.length  );
      //output.writeUTF(temp  );
      //output.writeChars(temp  );
      
      System.out.println("send output:"+bufout.length);
      
      c.close() ;
    }
    catch(IOException e){
      e.printStackTrace() ;
      
    }
    }

    public void commandAction(Command c, Displayable s) {
    
    }

}












0 0