Solved

keeping multiple communication sockets/ports open using siemens TC45 moduke

Posted on 2003-12-03
3
436 Views
Last Modified: 2012-08-14
Hi
 here is an explanation of the problem I am having.
The scenario can be duplicated as follows:

- I have a Main class that starts up a Thread that opens a server socket. The code for the listener thread is as follows:-

/*
 * ListenerThread.java
 *
 *  */

package com.applicationjunction.messenger;
import javax.microedition.io.*;
/**
 *
 * @author  Administrator
 */
public class ListenerThread extends Thread {
 
    private boolean running = false;
    private final Main main;
    //private WorkThread workThread;
 
    /** Creates a new instance of ListenerThread */
    public ListenerThread(Main main) {
        super();
        this.main = main;
    }
 
    public synchronized void start() {
        running = true;
        super.start();
    }
 
    public synchronized void kill() {
        running = false;
    }
 
    public void run() {
        try {
            StreamConnectionNotifier notifier = null;
            StreamConnection connection = null;
            java.io.InputStream is = null;
            java.io.OutputStream os = null;
         
            notifier = (StreamConnectionNotifier) Connector.open("serversocket://:9000",Connector.READ_WRITE,false);
            System.out.println("Waiting for connections on port 9000");
            while (running) {
                // wait for a incoming connection
                connection = notifier.acceptAndOpen();

                System.out.println("Connection received from " + connection.toString());
                os = connection.openOutputStream();
                is = connection.openInputStream();
                System.out.println("Connected to " + connection.toString());
                System.out.println("Sending \"OK\"...");
                // ready to read and write to the stream...
                os.write("ok".getBytes());
                os.flush();
                System.out.println("Message sent.");
                os.close();
                is.close();
                connection.close();
                connection = null;
            }
         
        }   catch (Exception err) {
            System.out.println("Something went wrong with the Listener Thread.");
            err.printStackTrace();
        }
    }
}

This causes the thread to listen for connections on port 9000. This works fine.

- The Main class then opens a socket connection to another module listening on port 9000. The code is as follows:-

        StreamConnection test1 = null;
        InputStream test1in = null;
        OutputStream test1out = null;
        try {
            test1 = (StreamConnection) Connector.open("socket://192.168.0.4:9000",Connector.READ_WRITE,false);
            test1in = test1.openInputStream();
            test1out = test1.openOutputStream();
           
            System.out.println((char) test1in.read());
            System.out.println((char) test1in.read());
           
            test1in.close();
            test1out.close();
            test1.close();
            test1 = null;
        } catch (IOException ioe) {
            System.err.println("Could not receive from other box");
        }

At this point the box is listening for incomming connections and trying to make an outgoing connection to a separate box.
After the socket connection is made, the server socket connection dies with the following stackTrace printout.

java.io.IOException
        at com.sun.cldc.io.j2me.serversocket.Protocol.acceptAndOpen(+4)
        at com.applicationjunction.messenger.ListenerThread.run(+37)

That is all I can give you to replicate the problem. All this is happening over a GPRS connection using the Siemens TC45 device.
I hope you can help me out.

0
Comment
Question by:gregkl
3 Comments
 
LVL 48

Accepted Solution

by:
Mikal613 earned 500 total points
ID: 9867054
First Make the Socket Connect and Then and then Listen for incoming connections
0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

MAC Filtering: MAC filtering is like handing a list of names to a doorman. If someone comes to the door and mentions a name, this name is checked by the doorman on his list and granted or denied access by this. This means that if someone menti…
DECT technology has become a popular standard for wireless voice communication. DECT devices are not likely to be affected by other electronic devices and signals because they operate in a separate frequency-band.
This Micro Tutorial will show you how to maximize your wireless card to its maximum capability. This will be demonstrated using Intel(R) Centrino(R) Wireless-N 2230 wireless card on Windows 8 operating system.
Viewers will learn how to connect to a wireless network using the network security key. They will also learn how to access the IP address and DNS server for connections that must be done manually. After setting up a router, find the network security…

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now