不更新了 不更新了在这里
新星创作者: Java技术领域
领域专家: 后端开发技术领域
2008-01-03 04:37:35
题目:编写一段Server程序和UserThread程序,目的是能够同时为多个顾客服务。顾客的请求表示为一个字符串,如果这个请求内容是“hello”,服务器将“hello”字符串返回给用户,否则其它请求则不响应。

最好给完整代码
...全文
56 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
tanlingyun 2008-01-03
  • 打赏
  • 举报
回复
上面有2个类贴上去没贴好
tanlingyun 2008-01-03
  • 打赏
  • 举报
回复
下面这个程序是我帮你在我以前的一个基础上改的,句法有点混乱,不过功能是实现了,其中S类是服务器端程序,ServerThread类用来处理多线程的,C类是客户机端程序。

S类如下:

import java.io.*;
import java.net.*;
import java.util.Vector;
public class S
{
static Vector brs = new Vector();
public static void main(String[] args)
{
ServerSocket server = null;
int numClient = 0;

new Thread(new Runnable(){
public void run()
{
BufferedReader s = new BufferedReader(new InputStreamReader(System.in));
while (true) {
try {
String temp = s.readLine();
String control = null;
int cc = 0;

if (temp.startsWith("to all ")) {
for (int i = 0; i < brs.size(); i++) {
BufferedWriter br = (BufferedWriter)brs.get(i);
if (br != null) {
br.write(temp + "\r\n");
br.flush();
}
}
}
else {
if (temp.matches("to \\d+ .*")) {
control = temp.substring(0, temp.indexOf(" ", 3) + 1);
cc = Integer.parseInt(control.substring(3, control.indexOf(" ", 3)));

BufferedWriter br = (cc > 0 && cc <= brs.size()) ? (BufferedWriter)brs.get(cc-1) : null;
if (br != null) {
br.write(temp + "\r\n");
br.flush();
}
else {
System.err.println("找不到客户端 " + cc);
listClients();
}
}
else {
System.err.println("命令错误。必须是to <numClient> <message> 或 to all <message>");
listClients();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}).start();

try
{
boolean flag = false;
server = new ServerSocket(4710);
while(true)
{
Socket client = server.accept();
PipedOutputStream pout = new PipedOutputStream();
for (int i = 0; i < brs.size(); i++) {
if (brs.get(i) == null) {
brs.set(i, new BufferedWriter(new OutputStreamWriter(pout)));
numClient = i + 1;
flag = true;
break;
}
}
if (!flag) {
brs.add(new BufferedWriter(new OutputStreamWriter(pout)));
numClient = brs.size();
}
PipedInputStream pin = new PipedInputStream(pout);
new ServerThread(pin, client, numClient).start();
System.out.println("Client" + numClient + " 连入");
}
}
catch(Exception e)
{
e.printStackTrace();
}
}

public static void listClients()
{
System.out.print("当前客户端列表:");
for (int i = 0; i < brs.size(); i++) {
if (brs.get(i) != null) {
System.out.print((i + 1) + " ");
}
}
System.out.println();
}
}


ServerThread类:

import java.io.*;
import java.net.*;
public class ServerThread extends Thread
{
Socket client = null;
int numClient;
PipedInputStream pin;
boolean done = false;

public ServerThread(PipedInputStream pin, Socket client, int num)
{
this.pin = pin;
this.client = client;
this.numClient = num;
}

public void run()
{
try
{
String line;
new Thread(new Runnable(){
public void run()
{
try {
BufferedReader is = new BufferedReader(new InputStreamReader(client.getInputStream()));
while (true) {
String info=is.readLine();
System.out.println("Client" + numClient + ":" + info);
if(info.equals("hello"))
{
PrintWriter pw=new PrintWriter(client.getOutputStream());
pw.println("hello");
pw.flush();
}
}
} catch (IOException e) {
done = true;
Li11_10.brs.set(numClient -1, null);
System.out.println("与 Client" + numClient + " 断开");
}
}
}).start();

BufferedReader sin = new BufferedReader(new InputStreamReader(pin));
PrintWriter os = new PrintWriter(client.getOutputStream());
line = sin.readLine();
String key = "to " + numClient + " ";
while(!done)
{
String words = line.substring(line.startsWith("to all ") ? "to all ".length() : key.length());
if (words != null) {
os.println(words);
os.flush();
System.out.println("Server " + key + ":" + words);
}
line = sin.readLine();
}
}
catch(Exception e) {
e.printStackTrace();
}
}
}


C类

import java.io.*;
import java.net.*;
public class C {

static Socket socket;
public static void main(String[] args) {
// TODO Auto-generated method stub
try
{
socket = new Socket("127.0.0.1", 4710);
new Thread(new Runnable(){
public void run()
{
BufferedReader is;
try {
is = new BufferedReader(new InputStreamReader(socket.getInputStream()));
while (true)
System.out.println("Server:" + is.readLine());
} catch (IOException e) {
e.printStackTrace();
}

}
}).start();

PrintWriter os = new PrintWriter(socket.getOutputStream(), true);
BufferedReader sin = new BufferedReader(new InputStreamReader(System.in));
String readLine;
readLine = sin.readLine();
while(!readLine.equals("bye"))
{
os.println(readLine);
System.out.println("Client:" + readLine);
readLine = sin.readLine();
}
os.close();
socket.close();
}
catch(Exception e)
{
System.out.println("Error" + e);
}
}

}
sunwei_07 2008-01-03
  • 打赏
  • 举报
回复
哈哈,LS观察还真仔细
LZ加个监听器看行不行
snake09003232 2008-01-03
  • 打赏
  • 举报
回复
~~~LZ哪来的那么多分啊?
到处都看到你的身影...这么多分还一星?

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧