You Are Here:

Community: Developer Discussion Boards

#1 Old Nokia 7650 and Socket communication problem - 2003-03-26, 18:27

Join Date: Mar 2003
Posts: 10
wauer
Offline
Registered User
Hi,
I try to develop a simple MIDlet for my Nokia 7650 that sets up a standard socket connection and sends a simple String message to the server.

OutputConnection con = (OutputConnection) Connector.open("socket://addr:4711");
DataOutputStream out = con.openDataOutputStream();
out.writeUTF("Midlet says: Hi!");
out.flush();
out.close();
con.close();

In my emulator everything works fine. Running the same MIDlet on my 7650 the writeUTF blocks. After closing the application the message is sent to the server.
What could be the reason for this strange behaviour?

Best regards,
Wolfgang
Reply With Quote

#2 Old 2003-03-27, 03:36

Join Date: Mar 2003
Posts: 393
nmittal's Avatar
nmittal
Offline
Regular Contributor
Sockets are officially not supported on 7650.

[N]/Forum Nokia
Reply With Quote

#3 Old 2003-03-27, 07:25

Join Date: Mar 2003
Posts: 10
wauer
Offline
Registered User
Thanks for your reaction!
Therefore this means than none of various the connection types provided by Connector will work (will be supported officially). Is that correct? Even it is not supported officially is there a possibility to get it work anyway?

Best regards,
Wolfgang
Reply With Quote

#4 Old 2003-03-31, 15:49

Join Date: Mar 2003
Posts: 4
v_v_nataraj
Offline
Registered User
Its working for me. How are you trying to read the message on other end?
Reply With Quote

#5 Old 2003-03-31, 17:10

Join Date: Mar 2003
Posts: 10
wauer
Offline
Registered User
Hi,

I just open an DataInputStream and perform a standard readUTF operation.

Socket connection = serverSocket.accept();
DataInputStream in = new DataInputStream(connection.getInputStream());

String str = in.readUTF();

I assume this should work.

Best regards,
Wolfgang
Reply With Quote

#6 Old 2003-04-01, 07:31

Join Date: Mar 2003
Posts: 4
v_v_nataraj
Offline
Registered User
Hi,

Given below is what i did. just check it out

Regards
Nataraj



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

public class Tcp01c extends MIDlet implements Runnable
{
private Thread t;
private Display display;
private Form form;
private Command exitCommand, getCommand;
private String connectionDetails = "socket://10.10.10.10:1801";
private StreamConnection sc;
private DataInputStream dis;
private DataOutputStream dos;
private TextField tbMain;
private static Tcp01c tcp01 = null;

private static int count=0;

public Tcp01c()
{
tcp01 = this;
//create the backCommand, okCommand and the exitCommand
exitCommand = new Command("Exit", Command.EXIT, 1);
getCommand = new Command("Start", Command.SCREEN, 1);
System.out.println("exit command and get command were created");
tbMain = new TextField("Enter server url", "socket://10.10.10.10:1801", 50, TextField.ANY);

//creating the form
form = new Form("Exchanging Msgs");
form.addCommand(exitCommand);
form.append(tbMain);
form.addCommand(getCommand);
System.out.println("exit command and get command were appended to the form");

//instantiating the listener
CommandListener listener = new CommandListener()
{
public void commandAction(Command c,Displayable d)
{
if(c==exitCommand)
{
System.out.println("exitCommand was pressed");
exit();
}
else if(c==getCommand)
{
System.out.println("start command pressed");
connectionDetails = tbMain.getString();
t = new Thread(tcp01);
t.start();
}
}


public void exit()
{
destroyApp(true);
notifyDestroyed();
}
} ;

//setting the listeners
form.setCommandListener(listener);

//getting the display object
display = Display.getDisplay(this);
}

public void startApp()
{
display.setCurrent(form);
System.out.println("form was set as the current screen");
}

public void pauseApp()

{
}

public void destroyApp(boolean cond)
{
display = null;
form = null;
exitCommand = null;
}



public void run()
{
try
{

//establishing a connection with the remote server

System.out.println("about to get a stream connection");

sc = (StreamConnection)Connector.open(connectionDetails,Connector.READ_WRITE);
System.out.println("input connection was created");
//input stream is created on top
//of the stream connection object

dis = sc.openDataInputStream();
System.out.println("input stream was created");

dos = sc.openDataOutputStream();
System.out.println("output stream was created");
}
catch(IOException ioe)
{
System.out.println("Unable to open conn/streams");
ioe.printStackTrace();
}


while(count < 10)
{
try
{

//send data
dos.write(("Message no. "+ count++ ).getBytes());
dos.write(10);
dos.flush();

//receiving data
byte []rb = new byte[1000];

int length = dis.read(rb);
System.out.println("BYTES RECEIVED "+length+" AND "+new String(rb,0,length));
//for(int k= 0;k<length;k++)
//{
// System.out.println("byte["+k+"]="+rb[k]);
//}

//displaying the received data
if(form.size()!=0)
{
form.delete(0);
}
form.append(new String(rb,0,length));
//form.append(new StringItem("DOLLAR NIS exchange rate is ", sb.toString()));

length = dis.read(rb);
System.out.println("BYTES RECEIVED "+length+" AND "+new String(rb,0,length));

//displaying the received data
if(form.size()!=0)
{
form.delete(0);
}
form.append(new String(rb,0,length));
//form.append(new StringItem("DOLLAR NIS exchange rate is ", sb.toString()));
}

catch(Exception e)
{
if(form.size()!=0)
{
form.delete(0);
}

form.append(new StringItem("problem:",e.getMessage()));
e.printStackTrace();
}
try{Thread.sleep(2000);}catch(Exception e){e.printStackTrace();}
}
try
{
//releasing resources that were allocated
if(dis!=null) dis.close();
if(dos!=null) dos.close();
if(sc!=null) sc.close();
}
catch(IOException ioe)
{
System.out.println("Could not free the resources");
ioe.printStackTrace();
}
}
}





-------------------------------------------------------------------------------
server:
import java.net.*;
import java.io.*;

public class Tcp01s
{

public static void main(String args[])
{

int port;
ServerSocket server_socket;
BufferedReader input=null;
BufferedWriter output=null;;

try
{
port = Integer.parseInt(args[0]);
}
catch (Exception e)
{
System.out.println("port = 1801 (default)");
port = 1801;
}

try
{

server_socket = new ServerSocket(port);
System.out.println("Server waiting for client on port " +
server_socket.getLocalPort());

// server infinite loop
while(true)
{
Socket socket = server_socket.accept();
System.out.println("New connection accepted " +
socket.getInetAddress() +
":" + socket.getPort());
input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
output = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
// print received data
try
{
String message = null;
while(true)
{
message="";

message = input.readLine();

System.out.println("GOT MESSAGE: "+message);
System.out.flush();
System.out.println("SENDING: "+"reply to:"+message);
output.write("reply to:"+message);
output.flush();
//output.close();
try{Thread.sleep(2000);}catch(Exception e){e.printStackTrace();}

System.out.println("SENDING: "+"reply 2 to:"+message);
output.write("reply 2 to:"+message);
output.flush();


}
}
catch (IOException e)
{
System.out.println(e);
}

// connection closed by client
try
{
socket.close();
System.out.println("Connection closed by client");
}
catch (IOException e)
{
System.out.println(e);
}
}
}

catch (IOException e)
{
System.out.println(e);
}
}
}
----------------------------------------------------------
Reply With Quote

#7 Old 2003-04-01, 15:44

Join Date: Mar 2003
Posts: 2,280
Location: Israel
shmoove
Offline
Forum Nokia Champion
To wauer:
Since sockets are not officially supported you can't expect all the methods to be working properly. If you say that the problem you have is with writeUTF(), then why not try a different method. I haven't tried using sockets so I can't tell you which methods work and which don't but I see that v_v_nataraj is only using write in his code (and not writeUTF), so why not try that?
Reply With Quote

#8 Old 2003-04-07, 12:48

Join Date: Mar 2003
Posts: 10
wauer
Offline
Registered User
To be honest I tried almost any combination of operations and connector classes. Thus it must be something else.
I'll try to get the posted source code running...

Best regards,
Wolfgang
Reply With Quote

#9 Old 2003-04-27, 16:28

Join Date: Apr 2003
Posts: 2
galnachum
Offline
Registered User
Wolfgang,

Did you try the code Nataraj posted ?
Is it working for you ?

Any other way to make the 7650 communicate over TCP/IP you have found ?

Gal.
Reply With Quote

#10 Old 2003-04-29, 13:56

Join Date: Mar 2003
Posts: 10
wauer
Offline
Registered User
Gal,

Unfortunately I didn't get the sample code working and have not found any other solution. In contrast to my own code I always get an IOException ("Unable to open conn/streams"). Strange...

Wolfgang
Reply With Quote
Reply « Previous Thread | Next Thread »
Display Modes
Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules

You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Forum Jump

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia