public class Client implements Runnable{ public void start() { Thread t = new Thread(this); t.start(); } public void run() { try { System.out.println("===================the client thread run"); sc = (SocketConnection) Connector.open("socket://localhost:"+PORT); si.setText("Connected to server"); dis = sc.openDataInputStream(); dos = sc.openDataOutputStream(); // Start the thread for sending messages - see Sender's main // comment for explanation sendID++; sender = new Sender(dos); //开始接受数据Oct.13 while(true){ }}其中Sender类是一个用来发送数据的线程extends Thread
import java.io.*; import java.net.*; import java.util.*;public class Server implements Runnable { public final static int MAX_CONNECTIONS = 50;//默认连接数50 public final static int DEFAULT_MAX_BACKLOG = 5;private void getIni(){ connections = new Vector(max_connections); //启动线程 Thread t=new Thread(this); t.start(); }//server run ------------------------------------------------------- public void run(){ }
public class Client implements Runnable{ //内部类 private String inok;public void init() { try { } catch(Exception e) { e.printStackTrace(); } }public void run(){ }public boolean getIni(){ //*** //启动线程 conn = new Vector(maxconn); Thread thrd = new Thread(this); thrd.start(); return true; } }//main---------------------------------------------------- public static void main(String args[]){ try { String cmdstr; Server ufs=new Server(); ufs.getIni(); Client C=Client(); //// 问题出在这里cannot resolve symbol: //// class Client at line 68, column 9 //// 这个内部类应该怎么调用? C.getIni(); } catch(Exception e) { System.out.println("Unable to connect to server!"); e.printStackTrace(); } } } Client 这个内部类应该怎么调用?
若Client是你的Server的内部类,就像我所看到的,你在main中可以这么写: Server ufs=new Server(); Server.Client client=ufs.new Client(); try this@
Thread t = new Thread(this);
t.start();
}
public void run() {
try {
System.out.println("===================the client thread run");
sc = (SocketConnection) Connector.open("socket://localhost:"+PORT);
si.setText("Connected to server");
dis = sc.openDataInputStream();
dos = sc.openDataOutputStream();
// Start the thread for sending messages - see Sender's main
// comment for explanation sendID++;
sender = new Sender(dos);
//开始接受数据Oct.13
while(true){
}}其中Sender类是一个用来发送数据的线程extends Thread
/*
* @(#)Sender.java 1.6 04/04/25
*
* Copyright (c) 2000-2004 Sun Microsystems, Inc. All rights reserved.
* PROPRIETARY/CONFIDENTIAL
* Use is subject to license terms
*/
package socket;import javax.microedition.midlet.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import java.io.*;public class Sender extends Thread { private OutputStream os;
private String message; public Sender(OutputStream os) {
this.os = os;
start();
} public synchronized void send(String msg) {
message = msg;
notify();
} public synchronized void run() { while (true) { // If no client to deal, wait until one connects
if (message == null) {
try {
wait();
} catch (InterruptedException e) {
}
} if (message == null) {
break;
} try {
os.write(message.getBytes());
os.write("\r\n".getBytes());
} catch (IOException ioe) {
ioe.printStackTrace();
} // Completed client handling, return handler to pool and
// for wait
message = null;
}
} public synchronized void stop() {
message = null;
notify();
}
}
我现在遇到一个问题, 客户端和服务器端是两个分开的类 Client.class
和Server.class因为程序要同为客户端和服务器端与C++ Socket 的服务器端和客户端,在两个端口通讯,我在整合JAVA 的Client.class 与 Server.class时, 我增加了一个 Frame.class(表单)Frame.clsee的文本筐输入的数据送往发送缓存,而这个缓存的数据必须要让Client.class 的线程能够扫描到,这个发送缓存区我应该怎么去定义两边才能兼顾呢?时不时把Client.class 作为Frame.class的一个子类这样好做点?请大侠们指教
http://community.csdn.net/Expert/topic/3706/3706109.xml?temp=.6670801
import java.net.*;
import java.util.*;public class Server implements Runnable { public final static int MAX_CONNECTIONS = 50;//默认连接数50
public final static int DEFAULT_MAX_BACKLOG = 5;private void getIni(){
connections = new Vector(max_connections);
//启动线程
Thread t=new Thread(this);
t.start();
}//server run -------------------------------------------------------
public void run(){ }
public class Client implements Runnable{ //内部类
private String inok;public void init() {
try { }
catch(Exception e) {
e.printStackTrace();
}
}public void run(){
}public boolean getIni(){
//***
//启动线程
conn = new Vector(maxconn);
Thread thrd = new Thread(this);
thrd.start();
return true; } }//main----------------------------------------------------
public static void main(String args[]){
try {
String cmdstr;
Server ufs=new Server();
ufs.getIni();
Client C=Client(); //// 问题出在这里cannot resolve symbol:
//// class Client at line 68, column 9
//// 这个内部类应该怎么调用?
C.getIni();
}
catch(Exception e) {
System.out.println("Unable to connect to server!");
e.printStackTrace();
}
} }
Client 这个内部类应该怎么调用?
Server ufs=new Server();
Server.Client client=ufs.new Client();
try this@