初学程序,自己学着做个基于TCP的Chatroom,就是实现简单的收发字符串,GUI还没写。
发现好些问题,都是些菜鸟级问题,请各位不吝赐教。谢谢Server和Client端的源码,分别贴在楼下。
解决方案 »
- 有没有比较好的jsp书,有se基础
- 搭建SSH项目时报了如下错误,引起其他项目也报同样的错,(SSH项目已删除),请大家指点点,谢谢
- tomcat配置BoneCP然后在JAVA中封装连接,使用ORACLE数据库
- 问个技术性的问题,web-inf可以保护网站中的某些文件.....
- 用java实现星座查询
- 绑定域名之后,session保存不了了??
- ant如何编译nutch2.x?
- 浏览器收藏javaweb的动态jsp页面
- 新人求教:关于ibatis的SqlMapClient,怎么避免异常声明?
- java 与 C#
- 请教:spring,activeMQ整合后如何实现Queue及Topic方式的消息发送
- 初学struts,出现org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper cannot be cast to com.mysql.jdbc.Connection
import java.net.*;
import java.util.*;public class S { Socket Sc = null; ReceiveAndSendtoClient Rastc = null; List<ReceiveAndSendtoClient> Thclients = new ArrayList<ReceiveAndSendtoClient>(); public static void main(String[] args) { (new S()).Connect(); } void Connect() {
try {
ServerSocket Ss = new ServerSocket(8888);
while (true) {
Sc = Ss.accept();
System.out.println("A client connected.");
Rastc = new ReceiveAndSendtoClient();
Thclients.add(Rastc);
new Thread(Rastc).start(); }
} catch (IOException e) {
e.printStackTrace();
} } class ReceiveAndSendtoClient implements Runnable { InputStream Is;
DataInputStream Dis; public void run() { while (true) {
try {
//接收客户端字符串
Is = Sc.getInputStream();
Dis = new DataInputStream(Is);
//向每个客户端发送字符串
for (int i = 0; i < Thclients.size(); i++) {
Thclients.get(i).send();
System.out.println(i);
}
} catch (IOException e) {
e.printStackTrace();
} } } void send() {
OutputStream Sos;
try {
Sos = Sc.getOutputStream();
DataOutputStream Dsos = new DataOutputStream(Sos);
Dsos.writeUTF(Dis.readUTF());
} catch (IOException e) {
e.printStackTrace();
}
} }}
import java.io.*;
import java.net.*;
import java.util.*;public class C {
Socket Cs; public static void main(String[] args) { (new C()).connect();
//(new Mainframe()).LaunchFrame(); } void connect() {
try {
Cs = new Socket("127.0.0.1", 8888);
//test print
OutputStream Os = Cs.getOutputStream();
DataOutputStream Dos = new DataOutputStream(Os);
Dos.writeUTF("asda");
this.ReceiveFromServer();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} } void ReceiveFromServer() {
InputStream Cis;
DataInputStream Cdis;
while (true) {
try {
Cis = Cs.getInputStream();
Cdis = new DataInputStream(Cis);
System.out.println(Cdis.readUTF());
} catch (IOException e) {
e.printStackTrace();
} } } class Mainframe {
void LaunchFrame(){
Frame f =new Frame();
f.setSize(300,200);
f.setVisible(true);
Button b = new Button("提交");
f.add(b);
}
}
}
1:目前的程序可以实现客户端的连接,可以实现第一个Client的正常连接。在第一个Client里能收到Client发送的字符串。但是后来的就不行了。尝试在Send方法中打印i值,结果只是第一个Client时出来一个0,后续就不打印了....
怀疑是Send方法,但是没看出来问题2:Server端的InputStream是定义在While(true)里好,还是在外面好,由什么区别吗?不太清楚,请赐教3:在做这个类(List <ReceiveAndSendtoClient> Thclients = new ArrayList <ReceiveAndSendtoClient>(); )的集合的时候,发现如果写成
list Al =new ArrayList();
Al.add(new ReceiveAndSendtoClient());
结果我在Al.get()方法中只能取到Object类的方法,可是我想取的是Send方法,请问错在哪了?
4:最弱的一个问题:我在client里写有一句//(new Mainframe()).LaunchFrame();
但是为什么编译出错呢?提示NO enlosing instance of type c ...
内部类不可以直接在main里做对象?
Server
import java.io.*;
import java.net.*;
import java.util.*;public class S { Socket Sc = null; ReceiveAndSendtoClient Rastc = null; //集合,用来存放线程类
List<ReceiveAndSendtoClient> Thclients = new ArrayList<ReceiveAndSendtoClient>(); public static void main(String[] args) {
//调用Connect方法。
(new S()).Connect(); } //建立本身服务器端连接,建立死循环不断接收客户端连接,
//每个客户端实现不断接收字符串,所以再有一个死循环,不断接收,
//为了实现多个客户端连接,采用线程
void Connect() {
try {
ServerSocket Ss = new ServerSocket(8888);
while (true) {
Sc = Ss.accept();
System.out.println("A client connected.");
Rastc = new ReceiveAndSendtoClient(); new Thread(Rastc).start();
Thclients.add(Rastc);
}
} catch (IOException e) {
e.printStackTrace();
} } //具体的线程类,
class ReceiveAndSendtoClient implements Runnable { InputStream Is;
DataInputStream Dis;
OutputStream Sos;
DataOutputStream Dsos;
public void run() { //不断循环接收字符串,且在接收的时候,将目前接收到的信息发送给每一个连接上来的客户段的Socket
//就是每一个连接上来的线程
try {
Is = Sc.getInputStream();
Dis = new DataInputStream(Is);
while (true) {
//接收客户端字符串
//向每个客户端发送字符串
for (int i = 0; i < Thclients.size(); i++) {
System.out.println(i);
Thclients.get(i).send();
}
}
} catch (IOException e) {
e.printStackTrace();
} } //send方法,发送字符串
void send() {
try {
Sos = Sc.getOutputStream();
Dsos = new DataOutputStream(Sos);
Dsos.writeUTF(Dis.readUTF());
} catch (IOException e) {
e.printStackTrace();
}
} }}
import java.io.*;
import java.net.*;
import java.util.*;public class C {
Socket Cs;
//拿到Mainframe的引用
Mainframe Mf;
public static void main(String[] args) { C c = new C();
c.connect();
c.initframe();
}//建立CS连接,在此Socket上,往外写一个字符串,同时接收服务器端字符串
void connect() {
try {
Cs = new Socket("127.0.0.1", 8888);
//test print
OutputStream Os = Cs.getOutputStream();
DataOutputStream Dos = new DataOutputStream(Os);
Dos.writeUTF("asda");
this.ReceiveFromServer();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} }
//接收服务器端的字符串,死循环接收
void ReceiveFromServer() {
InputStream Cis;
DataInputStream Cdis;
while (true) {
try {
Cis = Cs.getInputStream();
Cdis = new DataInputStream(Cis);
System.out.println(Cdis.readUTF());
} catch (IOException e) {
e.printStackTrace();
} } }
//运行可视化界面
void initframe(){
Mf = new Mainframe();
Mf.LaunchFrame();
}
//GUi的具体类
class Mainframe {
Frame LaunchFrame(){
Frame f =new Frame();
f.setSize(300,200);
f.setVisible(true);
Button b = new Button("提交");
f.add(b);
f.repaint();
return f;
}
}
}