Solved

keeping multiple communication sockets/ports open using siemens TC45 moduke

Posted on 2003-12-03
3
438 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

Now Available: Firebox Cloud for AWS and FireboxV

Firebox Cloud brings the protection of WatchGuard’s leading Firebox UTM appliances to public cloud environments. It enables organizations to extend their security perimeter to protect business-critical assets in Amazon Web Services (AWS).

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
WiFi Blackspot within home network 7 96
Non domain computers can't connect to wireless network using radius 7 74
Testing my WiFi 13 57
Setup small office network 1 58
This article is a step by step guide on how to create a basic PTP link using Ubiquiti airOS devices. This guide can be used on the following Ubiquiti AirMAX devices. Nanostation, Bullets, AirBridge, Nanobeam, NanoBridge to name a few. Please review …
Today sees the launch of a new case study, focusing on BYOD technologies we have been working with for some time now.  But with the advent of 802.11ac wireless technologies and the story behind our landmark developments, we would like to share this …
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…

756 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