我寫了一個客戶端服務器程序:使單個客戶端和服務器
{
import java.net.*;
class verify {
public static int serverPort=998;
public static int clientPort=999;
public static int buffer_size=1024;
public static DatagramSocket ds;
public static byte buffer[]=new byte[buffer_size];
public static void TheServer() throws Exception {
int pos=0;
while (true) {
int c=System.in.read();
switch(c) {
case -1:
System.out.println("server quits");
return;
case '\r':
break;
case '\n':
ds.send(new DatagramPacket(buffer,pos,InetAddress.getLocalHost(),clientPort)) ;
pos=0;
break;
default:
buffer[pos++] = (byte) c;
}
}
}
public static void TheClient()throws Exception {
while(true) {
DatagramPacket p=new DatagramPacket(buffer,buffer.length);
ds.receive(p);
System.out.println(new String(p.getData(),0,p.getLength()));
}
}
public static void main(String args[]) throws Exception{
if(args.length==1) {
ds=new DatagramSocket(serverPort);
TheServer();
} else {
ds=new DatagramSocket(clientPort);
TheClient();
}
}
}
}
我可以讓我的PC服務器和client.If既肯定請出建議的方式。
請使用代碼格式功能。您帖子中的代碼很難閱讀。 – Mudassir 2011-03-23 05:56:49
@ Mudassir改進了代碼 – 2011-03-23 06:10:23