⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 client.java

📁 Socket程序
💻 JAVA
字号:
//Client.java		2004/04/01	tanaimport javax.microedition.midlet.*;import javax.microedition.io.*;import javax.microedition.lcdui.*;import javax.wireless.messaging.*;import java.io.*;public class Client implements CommandListener , Runnable {    private SMSSocketTest parent;    private Display display;    private Form f;    int NUM = 3;    TextField[] tf;        private boolean stop;    Command sendCommand = new Command("Send", Command.ITEM, 1);    // sms receive    Command testCommand = new Command("RecvSMS", Command.OK, 1);        //parameters used in socket connection     StringItem[] strItem;    String[] port = { "12100", "12101", "12102" };    InputStream[] is;    OutputStream[] os;    SocketConnection[] sc;    Sender[] sender;    ClientThread[] cth;    // sms receive    SmsRecv smsReceiver;        public Client(SMSSocketTest m)     {        parent = m;        display = Display.getDisplay(parent);        f = new Form("Socket Client");                strItem = new StringItem[ NUM ];        is = new InputStream[ NUM ];        os = new OutputStream[ NUM ];        sc = new SocketConnection[ NUM ];        sender = new Sender[ NUM ];        cth = new ClientThread[ NUM ];        tf = new TextField[ NUM ];                for( int i=0; i<NUM; i++ )        {            String tmp = "Port" + i + ":";            strItem[i] = new StringItem( tmp, " " );            tf[i] = new TextField( "Send:", "", 30, TextField.ANY );            f.append( strItem[i] );            f.append( tf[i] );        }                // sms receive        f.addCommand(testCommand);        f.addCommand(sendCommand);        f.setCommandListener(this);                // sms receive        smsReceiver = new SmsRecv( f, display );    }       public void start()     {    	display.setCurrent(f);    	    	for( int i=0; i<NUM; i++ )    	{    		cth[i] = new ClientThread( i );        	cth[i].start();        }    }        public void commandAction(Command c, Displayable s)     {        if (c == sendCommand && !parent.isPaused())         {            for( int i=0; i<NUM; i++ )            {            	sender[i].send( tf[i].getString() );            }        }        if (c == Alert.DISMISS_COMMAND )         {            parent.notifyDestroyed();            parent.destroyApp(true);        }                // sms receive         if ( c==testCommand )        {       	    	Thread th = new Thread( this );       	    	th.start();        }    }        // sms receive    public void run()    {    	smsReceiver.start();    }            public void stop() {        try {            stop = true;            for( int i=0; i<NUM; i++ )	    {	    	sender[i].stop();	    	if (is[i] != null) 	    	{	                is[i].close();        	}            	if (os[i] != null)             	{                	os[i].close();            	}            	if (sc[i] != null)             	{                	sc[i].close();            	}            }        }        catch (IOException ioe) {        }    }            class ClientThread extends Thread    {    	int index;    	    	public ClientThread( int num )    	{    		index = num;    	}    	    	public void run()     	{        	try {        		String urlString = "socket://localhost:" + port[index];			sc[index] = (SocketConnection) Connector.open(urlString);            		strItem[index].setText("Connected to server");            		is[index] = sc[index].openInputStream();            		os[index] = sc[index].openOutputStream();          	        sender[index] = new Sender(os[index]);            		// Loop forever, receiving data            		while (true)             		{                		StringBuffer sb = new StringBuffer();                		int c = 0;                		while (((c = is[index].read()) != '\n') && (c != -1))                 		{                    			sb.append((char) c);                		}                		// Display message to user                		strItem[index].setText("Message received - " + sb.toString());            		}        	} catch (ConnectionNotFoundException cnfe)         	{            		Alert a = new Alert("Client", "Please run Server MIDlet first", null, AlertType.ERROR);            		a.setTimeout(Alert.FOREVER);            	//	a.setCommandListener(this);            		display.setCurrent(a);        	} catch (IOException ioe)         	{            		if (!stop)             		{                		ioe.printStackTrace();            		}        	} catch (Exception e)         	{            		e.printStackTrace();        	}    	}    }}    		

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -