×

Discussion Board

Results 1 to 3 of 3
  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    5

    broadcast messages

    ChatServer:
    Code:
    import java.net.*;
    import java.io.*;
    import java.util.*;
    
    public class ChatServer {
    	private static ServerSocket serverSocket;
    	private static int port;
    
    	private Hashtable ht = new Hashtable();
    
      public static void main(String[] args) throws Exception {
        if (args.length == 0) {
          System.out.println("Usage: java ChatServer [port]");
          System.exit(1);
        }
    		port = Integer.parseInt(args[0]) ;
    		new ChatServer();
    	}
    
    	public ChatServer() throws IOException {
    		Socket clientSocket;
    
    		try {
    			serverSocket = new ServerSocket(port);
    	
    			System.out.println("Chat Server started at: " + serverSocket.getInetAddress().getLocalHost() + ":" + serverSocket.getLocalPort());
    	
    			while (true) {
    				clientSocket = serverSocket.accept();
    				
    				System.out.println("Connection from: " + clientSocket);
    				
    				DataOutputStream outData = new DataOutputStream(clientSocket.getOutputStream());
    				
    				ht.put(clientSocket, outData);
    	
    				Thread thread = new Thread(new ServerThread(clientSocket, ht));
    
    				thread.start();
    			}
    		}
        catch (IOException ex) {
    			ex.printStackTrace();
        }
    	}
    }
    
    class ServerThread extends Thread implements Runnable {
    	private Socket clientSocket;
    	private Hashtable ht;
    
    	public ServerThread(Socket clientSocket, Hashtable ht) {
    		this.clientSocket = clientSocket;
    		this.ht = ht;
    	}
    
    	public void run() {
    		DataInputStream inData;
    
    		try {
    			inData = new DataInputStream(clientSocket.getInputStream());
    			
    			while (true) {
    				String message = inData.readUTF();
    				
    				System.out.println("Message from: " + message);
    
    				synchronized(ht) {
    					for (Enumeration e = ht.elements(); e.hasMoreElements(); ) {
    						DataOutputStream outData = (DataOutputStream)e.nextElement();
    			
    						try {
    							outData.writeUTF(message);
    						} 
    				    catch (IOException ex) {
    							ex.printStackTrace();
    				    }
    					}
    				}
    			}
    		} 
        catch (IOException ex) {
        }
    		finally {
    			synchronized(ht) {
    				System.out.println("Remove connection: " + clientSocket);
    		
    				ht.remove(clientSocket);
    		
    				try {
    					clientSocket.close();
    				} 
    		    catch (IOException ex) {
    		    }
    			}
    		}
    	}
    }
    client:
    Code:
    import javax.microedition.lcdui.*;
    import javax.microedition.midlet.*;
    import javax.microedition.io.*;
    import java.util.*;
    import java.io.*;
    
    public class broadcast extends MIDlet implements CommandListener,Runnable
    {
    	private Form f;
    	private TextField name, say;
    	private Command submit;
    	private Display d;
    	private NetWork nw;
    	private Thread timer;
    	public broadcast()
    	{
    		d = Display.getDisplay(this);
    
    		f = new Form("broadcast");
    		name=new TextField("your nickname","",10, TextField.ANY);
    		say = new TextField("say?", "", 50, TextField.ANY);
    		submit = new Command("Submit", Command.OK, 1);
    		f.append(name);
    		f.append(say);
    		f.addCommand(submit);
    		f.setCommandListener(this);
    
    		
    	}
    
    	protected void startApp()
    	{
    		d.setCurrent(f);
    	}
    
    	protected void pauseApp() 
    	{	}
    
    	protected void destroyApp(boolean arg0)
    	{		}
    
    	public void commandAction(Command c, Displayable d)
    	{
    		if (c == submit)
    		{
    			nw = new NetWork(this);
    			nw.get(name.getString(), say.getString());
    			nw.start();
    
    			Thread timer = new Thread(this);
    			timer.start();
    				
    			
    		}
    	}
    	public void run()
    	{
    		try
        	{
    		SocketConnection sc = (SocketConnection)Connector.open("socket://127..0.0.1:1234");
    		StringBuffer sb = new StringBuffer();
    		sb = new StringBuffer();
    			while (true)
    			{
    			
    				
    				DataInputStream is = (DataInputStream)sc.openDataInputStream();
    				//is = c.openInputStream();
    				int ch;
    				
    				while ((ch = is.read()) != -1)
    				{
    					sb.append((char)ch);
    				}
    			
    				f.append(sb.toString() + "\n");
    				sb.delete(0, sb.length() - 1);
    				is.close();
    
    
    				
    				//long time = 1000;
    				//Thread.sleep(time);
    			}
    		sc.close();
    		}
    		catch (Exception e)
    		{ }
              
    	} 
    
    
    	public class NetWork implements Runnable
    	{
    		broadcast midlet;
    		private Display display;
    
    		String name;
    		String say;
    
    
    		public NetWork(broadcast midlet)
    		{
    			this.midlet = midlet;
    			display = Display.getDisplay(midlet);
    		}
    
    		public void start()
    		{
    			Thread t = new Thread(this);
    			t.start();
    		}
    
    		public void run()
    		{
    			StringBuffer sb = new StringBuffer();
    			try
    			{
    				SocketConnection sc = (SocketConnection)Connector.open("socket://127.0.0.1:1234");
    
    				DataOutputStream os = (DataOutputStream)sc.openDataOutputStream();
    
    
    				os.writeUTF(name.trim());
    				os.writeUTF(say.trim());
    				os.flush();
    				os.close();
    
    				// Get the response from the servlet page.
    
    					DataInputStream is = (DataInputStream)sc.openDataInputStream();
    					//is = c.openInputStream();
    					int ch;
    					sb = new StringBuffer();
    					while ((ch = is.read()) != -1)
    					{
    						sb.append((char)ch);
    
    					}
    					broadcast.this.f.append(sb.toString() + "\n");
    
    					is.close();
    				
    
    				sc.close();
    			}
    			catch (Exception e)
    			{
    				
    			}
    		}
    
    		public void get(String name, String say)
    		{
    			this.name = name;
    			this.say = say;
    		}
    
    	}; 	 
    }
    what's wrong?

  2. #2
    Super Contributor
    Join Date
    Dec 2005
    Location
    Europe/Poland/Warsaw
    Posts
    1,697

    Re: broadcast messages

    hi funkin,

    what's wrong with "what"?
    can you post short note?
    thanks,
    regards,
    Peter

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    5

    Re: broadcast messages

    Send message to Server is fine
    but form can't append subString(timer thread)

Similar Threads

  1. Receiving CBS (Cell Broadcast Service) Messages with Java
    By Entropy in forum Mobile Java Networking & Messaging & Security
    Replies: 7
    Last Post: 2008-09-15, 15:21
  2. API or other means to read Cell Broadcast messages by an application
    By celltick in forum Symbian Networking & Messaging (Closed)
    Replies: 1
    Last Post: 2006-10-02, 07:34
  3. Replies: 0
    Last Post: 2005-11-16, 10:37
  4. receive cell broadcast messages on 3650
    By moeminhtun in forum Symbian
    Replies: 0
    Last Post: 2003-06-03, 06:08
  5. Retrieving messages from ME/GSM modem
    By skshambhu in forum General Messaging
    Replies: 1
    Last Post: 2002-10-23, 10:53

Posting Permissions

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