帮帮我吧!!!!!

sungj09173 2009-12-18 11:12:33
import java.io.IOException;
import java.io.*;
import java.io.OutputStream;

import java.net.*;
import java.io.DataOutputStream;

import java.io.InputStream;


import java.util.LinkedList;
import java.util.List;

public class EchoServer {


private int isOk;
private ServerSocket ss = null;




class ReceiveThread extends Thread {

private Socket socket = null;


public ReceiveThread(Socket socket) {
super("ReceiveThread");
this.socket = socket;

}
public void run() {
InputStream in = null;
while (true) {

try {
in = socket.getInputStream();
InetAddress ip=socket.getInetAddress();
String string_ip=ip.toString().substring(1);
String temp;
System.out.println("连接ip"+ip);
BufferedReader br=new BufferedReader(new InputStreamReader(in));

while((temp=br.readLine())!=null)
{

System.out.println("receive client message :"+ temp);
if(!temp.equals(""))
{
OutputStream out = socket.getOutputStream();
PrintWriter pw=new PrintWriter(out,true);
pw.println("1");
String divide[]=temp.split("/");
Sql log =new Sql();

if(log.Logo(divide[0], divide[1])==1)
{

log.update_Ip(string_ip,divide[0]);
}
}


}
} catch (IOException e) {

try {socket.close();
in.close();
//dis.close();
} catch (IOException e1) {
e1.printStackTrace();
}

e.printStackTrace();
}
}

}
}
public void send(Socket socket) throws IOException {




}

public class AcceptSocketThread extends Thread {
public AcceptSocketThread() {
super("AcceptSocketThread");
}

public void run() {
try {
ss = new ServerSocket(6000);

while (true) {
Socket socket = ss.accept();



new ReceiveThread(socket).start();

}
} catch (IOException e) {
e.printStackTrace();
try{
ss.close();
}
catch(IOException e2)
{}

}

}
}

public void start() {
new AcceptSocketThread().start();

}


}


还是把全部代码贴出来吧!!
客户端没问题,关键是服务器的问题。OutputStream out = socket.getOutputStream();
PrintWriter pw=new PrintWriter(out,true);
pw.println("1");
这段代码已经是执行了,但是客户端收不到,如果把它放到while的前面又行
...全文
105 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
SambaGao 2009-12-19
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 lcymsn 的回复:]
ServerSocket s = new ServerSocket(9527);
Socket s1 = s.accept();
OutputStream os = s1.getOutputStream();
DataOutputStream dos = new DataOutputStream(os);
dos.writeUTF("text");
这样写试试    仅供参考
[/Quote]

用DataOutputStream 这个流的UTF-8试试。以前我测试通过。
JavaAlpha 2009-12-19
  • 打赏
  • 举报
回复
JavaAlpha 2009-12-19
  • 打赏
  • 举报
回复
那就在while外面定义。

在while里面定义了,范围就只能在while里面。
更何况在while里面定义。每次循环就定义一个对象。这样不好。

注意范围问题。
sean1203 2009-12-19
  • 打赏
  • 举报
回复
OutputStream out = socket.getOutputStream();
PrintWriter pw=new PrintWriter(out,true);
pw.write("1");
No-zero 2009-12-18
  • 打赏
  • 举报
回复
ServerSocket s = new ServerSocket(9527);
Socket s1 = s.accept();
OutputStream os = s1.getOutputStream();
DataOutputStream dos = new DataOutputStream(os);
dos.writeUTF("text");
这样写试试 仅供参考

62,620

社区成员

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

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