Доброго времени суток! в эмуляторе все работает, а на телефоне nokia 6233 нет... приложение должно работать так: 1. соединяется с сервером, который является ретранслятором(TCP). 2. запрашивает соединение с другим сервером по имени. 3. отправляет запросы на соединенный сервер. 1 и 2 работают. в 3 проблема, данные не пишутся в поток, как я думаю. RetranslatorClient | Код | /* * To change this template, choose Tools | Templates * and open the template in the editor. */
package MyNet;
import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import javax.microedition.io.*;
/** * * @author uranpro */ public class RetranslatorClient {
final String ENCODING = "UTF-8"; final String SERVER_NAME = "HomeRemoteTorrent"; final String SERVER_IP = "***"; final int SERVER_PORT = ***; final int BUFFER_SIZE = 1024;
InputStream inputStream; OutputStream outputStream;
public InputStream getInputStream(){ return inputStream; }
public OutputStream getOutputStream(){ return outputStream; }
public SocketConnection getConnection() throws IOException{
String qryStr = "connect$" + SERVER_NAME; byte[] buffer = new byte[BUFFER_SIZE]; int recv = -1;
SocketConnection sc = (SocketConnection)Connector. open("socket://" + SERVER_IP + ":" + SERVER_PORT);
sc.setSocketOption(sc.DELAY, 0); sc.setSocketOption(sc.KEEPALIVE, 0); sc.setSocketOption(sc.RCVBUF, BUFFER_SIZE); sc.setSocketOption(sc.SNDBUF, BUFFER_SIZE);
inputStream = sc.openInputStream(); outputStream = sc.openOutputStream();
outputStream.write(qryStr.getBytes(ENCODING));
recv = inputStream.read(buffer);
if (recv > 0){ String response = new String(buffer, 0, recv, ENCODING);
if (response.equals("ok")) return sc; } else throw new IOException("Client disconnected"); return null; }
}
|
RemoteTorrentClient(3 часть) | Код | /* * To change this template, choose Tools | Templates * and open the template in the editor. */
package MyNet;
import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import javax.microedition.io.SocketConnection; import MyNetUtils.CSBitConverter; import java.util.Vector;
/** * * @author uranpro */ public class RemoteTorrentClient {
static final String ENCODING = "UTF-8";
static String[] split(String original,String separator) { Vector nodes = new Vector(); // Parse nodes into vector int index = original.indexOf(separator); while(index >= 0) { nodes.addElement( original.substring(0, index) ); original = original.substring(index+separator.length()); index = original.indexOf(separator); } // Get the last node nodes.addElement( original ); // Create split string array String[] result = new String[ nodes.size() ]; if( nodes.size() > 0 ) { for(int loop = 0; loop < nodes.size(); loop++) result[loop] = (String)nodes.elementAt(loop); } return result; }
SocketConnection client; InputStream is; OutputStream os; String userLogin; String userPassword;
public RemoteTorrentClient(SocketConnection client, InputStream inputStream, OutputStream outputStream, String userLogin, String userPassword){ this.client = client; this.userLogin = userLogin; this.userPassword = userPassword; this.is = inputStream; this.os = outputStream; }
public void download(int id) throws IOException{ String requestStr = "download$" + userLogin + "$" + userPassword + "$" + id; os.write(requestStr.getBytes(ENCODING)); }
public String[] searchRequest(String searchContent) throws IOException{ String requestStr = "search$" + userLogin + "$" + userPassword + "$" + searchContent;
os.write(CSBitConverter.addBytesLength(requestStr.getBytes(ENCODING)));
byte[] lengthBytes = new byte[4];
int recv = is.read(lengthBytes);
int length = CSBitConverter.bytesToInt(lengthBytes);
byte[] data = new byte[length];
byte[] buffer = new byte[1024];
recv = -1; int received = 0; while (received < length && (recv = is.read(buffer)) != -1){ System.arraycopy(buffer, 0, data, received, recv); received += recv; }
String[] result = split(new String(data, ENCODING), "\r\n");
return result; }
}
|
CSBitConverter | Код | public final class CSBitConverter {
private CSBitConverter(){
}
public static byte[] intToBytes(int i) { byte[] bytes = new byte[4]; bytes[0] = (byte) (i & 0x00FF); bytes[1] = (byte) ((i >> 8) & 0x000000FF); bytes[2] = (byte) ((i >> 16) & 0x000000FF); bytes[3] = (byte) ((i >> 24) & 0x000000FF); return bytes; }
public static byte[] addBytesLength(byte[] data){ byte[] outBytes = new byte[data.length + 4]; System.arraycopy(intToBytes(data.length), 0, outBytes, 0, 4); System.arraycopy(data, 0, outBytes, 4, data.length); return outBytes; }
public static int bytesToInt(byte[] b){ return (b[3] << 24) + ((b[2] & 0xFF) << 16) + ((b[1] & 0xFF) << 8) + (b[0] & 0xFF); } }
|
Как пользуюсь | Код | RetranslatorClient rc = new RetranslatorClient(); try { SocketConnection client = rc.getConnection();
RemoteTorrentClient rtc = new RemoteTorrentClient(client, rc.getInputStream(), rc.getOutputStream(), "***", "***"); searchResults = rtc.searchRequest(searchContent);
if (list != null){ list.deleteAll(); for (int i = 0; i<searchResults.length; i++) list.append(searchResults[i], null); } } catch (IOException ex){ lastError = ex.getMessage(); throw ex; }
|
ошибок нет, приложение зависает, но когда я выхожу из приложения, то пишет "write error" Это сообщение отредактировал(а) uranpro - 26.7.2010, 17:59
--------------------
I want a perfect soul
|