1.编写完整的基于Socket的多客户/服务器通信程序
import java.io.*;
import java.net.*;
public class MultiTalkServer{
static int clientnum=0;
public static void main(String args[]) throws IOException {
ServerSocket serverSocket=null;
boolean listening=true;
try{
serverSocket=new ServerSocket(4700);
}catch(IOException e) {
System.out.println("Could not listen on port:4700.");
System.exit(-1);
}
while(listening){
new ServerThread(serverSocket.accept(),clientnum).start();
clientnum++;
}
serverSocket.close();
}
}
package MultiSocket;
import java.io.*;
import java.net.*;
public class MultiTalkServer{
static int clientnum=0;
public static void main(String args[]) throws IOException {
ServerSocket serverSocket=null;
boolean listening=true;
try{
serverSocket=new ServerSocket(4700);
}catch(IOException e) {
System.out.println("Could not listen on port:4700.");
System.exit(-1);
while(listening){
new ServerThread(serverSocket.accept(),clientnum).start();
clientnum++;
}
serverSocket.close();
}
}
package MultiSocket;
import java.io.*;
import java.net.*;
public class MultiTalkServer{
static int clientnum=0;
public static void main(String args[]) throws IOException {
ServerSocket serverSocket=null;
boolean listening=true;
try{
serverSocket=new ServerSocket(4700);
}catch(IOException e) {
System.out.println("Could not listen on port:4700.");
System.exit(-1);
}
while(listening){
new ServerThread(serverSocket.accept(),clientnum).start();
clientnum++;
}
serverSocket.close();
}
2.编写完整的基于数据报的多客户/服务器通信程序
import java.io.*;
import java.net.*;
public class QuoteClient {
public static void main(String[] args) throws IOException{
DatagramSocket socket=new DatagramSocket();
BufferedReader sin = new BufferedReader(new InputStreamReader(System.in));
String readLine;
InetAddress address=InetAddress.getByName("127.0.0.1");
while(!(readLine = sin.readLine()).equals("bye")) {
byte[] buf = readLine.getBytes();
DatagramPacket packet=new DatagramPacket(buf, buf.length, address, 4445);
socket.send(packet);
buf = new byte[256];
packet=new DatagramPacket(buf,buf.length);
socket.receive(packet);
buf = packet.getData();
String received=new String(buf);
System.out.println("Quote of the Sever: "+received );
}
socket.close();
}
}
import java.io.*;
import java.net.*;
class QuoteServerThread extends Thread
{
protected DatagramSocket socket=null;
protected BufferedReader in=null;
protected boolean moreQuotes=true;
public QuoteServerThread() throws IOException {
this("QuoteServerThread");
}
public QuoteServerThread(String name) throws IOException {
super(name);
in= new BufferedReader(new InputStreamReader(System.in));
}
public void run()
{
while(moreQuotes) {
try{
byte[] buf=new byte[256];
DatagramPacket packet=new DatagramPacket(buf,buf.length);
socket.receive(packet);
System.out.println("Client : "+new String(packet.getData()));
String dString= in.readLine();
if(dString.equals("bye")){moreQuotes=false;}
buf=dString.getBytes();
InetAddress address=packet.getAddress();
int port=packet.getPort();
packet=new DatagramPacket(buf,buf.length,address,port);
socket.send(packet);
}catch(IOException e) {
e.printStackTrace();
moreQuotes=false;
}
}
socket.close();
}
}
public class QuoteServer{
public static void main(String args[]) throws java.io.IOException
{
new QuoteServerThread().start();
}
}