有人可以用一些代码帮助我吗? [英] Could someone help me please with some code?

查看:85
本文介绍了有人可以用一些代码帮助我吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

import java.net.Socket;

import java.io.BufferedInputStream;

import java.io.BufferedOutputStream;

import java .io.IOException;



/ **

* ChatConnection在聊天服务器和
$ b之间实现连接$ b *单个聊天客户端。连接将作为单独的线程运行。

* /

公共类ChatConnection扩展线程{



/ **

*此连接将收听的插座

* /

/ **

*与此连接相关联的服务器。

* /

ChatServer服务器;

/ **

*输出流将用于向我们的客户写入消息

* /

BufferedOutputStream out;

/ **

*从我们的客户端读取和接收消息的输入流

* /

BufferedInputStream in;



/ **

*创建一个新的ChatConnection。套接字和服务器已初始化

* @param s此连接将侦听的套接字

* @param svr此连接与之关联的服务器

* /

公共ChatConnection(套接字s,ChatServer svr){

socket = s;

server = svr; < br $>
}



/ **

*覆盖Thread的run()方法。此方法将读取来自

* out客户端的输入并将该输入广播到此ChatServer的所有客户端。

*所有输出都使用服务器的MultiOutputStream进行广播>
*维持。

* /

public void run(){

int numBytesRead;

byte [] buf = new byte [256];

try {

//从套接字获取输入流

/ **

*输入您的代码这里

* /

//从我们的插座获取输出流

/ * *

*输入您的代码这里

* /

//将我们的输出流添加到服务器的MultiOutputStream

server.addOutputStream(out);

System.out.println(New ChatConnection now reading ...);

//循环读取输入流

while((numBytesRead = in.read(buf,0,buf.length))> -1){

System.out.print(Broadcast:+ new String(buf,0,numBytesRead));

//广播我们收到的所有内容

server.broadcast(buf ,numBytesRead);

}

} catch(IOException e){

}终于{

//与我们的客户端的连接已经关闭所以

//我们需要从服务器上移除我们的输出流

// MultiOutputStream并关闭我们的套接字。

尝试{

if(out!= null){

server.removeOutputStream(out);

}

if(socket!= null){

//关闭套接字

/ **

*输入你的代码这里

* /

}

} catch(IOException e){}

}

}

}



我的尝试:



我已经尝试了一切,似乎没有什么对我有用,我还有不到12个小时才能得到这个已完成。

import java.net.Socket;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;

/**
* A ChatConnection implements a connection between the chat server and a
* single chat client. The connection will run as a separate Thread.
*/
public class ChatConnection extends Thread{

/**
* The socket on which this connection will listen
*/
/**
* The server that this connection is associated with.
*/
ChatServer server;
/**
* The output stream that will be used to write messages to our client
*/
BufferedOutputStream out;
/**
* The input stream to read and receive messages from our client
*/
BufferedInputStream in;

/**
* Create a new ChatConnection. The socket and the server are initialised
* @param s The socket on which this connection will listen
* @param svr The server that this connection is associated with
*/
public ChatConnection(Socket s, ChatServer svr) {
socket = s;
server = svr;
}

/**
* Override the run() method of Thread. This method will read input from
* out client and broadcast that input to all clients of this ChatServer.
* All output is broadcast using the MultiOutputStream that the Server
* maintains.
*/
public void run() {
int numBytesRead;
byte[] buf = new byte[256];
try {
// Get the input stream from our socket
/**
* Enter your code Here
*/
// Get the output stream from our socket
/**
* Enter your code Here
*/
// Add our output stream to the server's MultiOutputStream
server.addOutputStream(out);
System.out.println("New ChatConnection now reading...");
// Loop to read from the input stream
while ((numBytesRead = in.read(buf,0,buf.length)) > -1) {
System.out.print("Broadcast: " + new String(buf,0,numBytesRead));
// broadcast everything we receive
server.broadcast(buf,numBytesRead);
}
} catch (IOException e) {
} finally {
// The connection to our client has been closed so
// we need to remove our output stream from the server's
// MultiOutputStream and also close our socket.
try {
if (out != null) {
server.removeOutputStream(out);
}
if (socket != null) {
//Close the Socket
/**
* Enter your code Here
*/
}
} catch (IOException e) {}
}
}
}

What I have tried:

I have tried everything and nothing seems to be working for me, i have less than 12 hours to get this completed.

推荐答案

你发布的是一个框架,大概是你的导师提供的 - 它充满了有用的部分,例如:

What you have posted is a framework that presumably your tutor provided - it's full of helpful bits such as:
// Get the input stream from our socket
/**
* Enter your code Here 
*/

它们正在告诉你该做什么,以及在哪里做。

这是你的作业:你应该考虑它和你的课程所涵盖的材料并将其应用到另一个。

在你做之前,我们无法给你提供任何帮助:我们不是来做你的功课:这是有原因的。它就是为了让你思考你被告知的事情,并试着理解它。它也在那里,以便您的导师可以识别您身体虚弱的区域,并将更多的注意力集中在补救措施上。



亲自尝试,你可能会发现它不是和你想的一样困难!



如果遇到具体问题,请询问相关问题,我们会尽力提供帮助。但是我们不会为你做这一切!



拼写[/ edit]

which are telling you exactly what to do, and where to do it.
This is your homework: you are expected to think about it and the material covered in your course and apply the one to the other.
Until you do, there is no help we can give you: we are not here to do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think!

If you meet a specific problem, then please ask about that and we will do our best to help. But we aren't going to do it all for you!

[edit]typo[/edit]


这篇关于有人可以用一些代码帮助我吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆