使用J2ME进行套接字编程

时间:2010-10-25 17:27:21

标签: sockets java-me tcp

如何使用J2ME连接到TcpServer(使用ip&端口)?我想向服务器发送请求并获得回复!

2 个答案:

答案 0 :(得分:2)

使用此代码解决了我的问题:

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

       StreamConnection connection = (StreamConnection) 
         Connector.open("socket://IP:Port");

       PrintStream output = 
         new PrintStream(connection.openOutputStream());

       output.println("Request");
       output.flush();

       InputStream in = connection.openInputStream();
       int ch;
       String recx;

       while( ( ch = in.read() ) != 13 )
       {
         recx = recx + (char)ch;  
       }


       in.close();
       output.close();
       connection.close();

答案 1 :(得分:1)