求助:socket!

auron01 2004-08-24 03:18:23
我写了一个socket的程序,server每秒向client传艺个随机数,第一次启动,一切正常,然后,关了client(此时server仍工作),再开,就不能读取数据了。
源码如下,高手帮我看看:
server:
import java.net.*;
import java.io.*;
/**
*
* @author Administrator
*/
public class SocketPost extends Thread{
ServerSocket serverSocket;
Socket socket;
OutputStream os;
DataOutputStream dos;
PrintStream ps;
/** Creates a new instance of SocketPost */
public SocketPost() {
}

public synchronized void post(){
try{
serverSocket=new ServerSocket(7086);
socket=serverSocket.accept();
os=socket.getOutputStream();
ps=new PrintStream(os);
start();
}catch(Exception ex){
ex.printStackTrace();
}
}

public void run(){
try{
while(true){
ps.println((int)Math.round(Math.random()*100));
//System.out.println((int)Math.round(Math.random()*100));
sleep(1000);
}
}catch(Exception ex){
ex.printStackTrace();
}
}
public static void main(String[] args) {
// TODO code application logic here
SocketPost sp=new SocketPosr();
sr.post();
}

}

client:
import java.net.*;
import java.io.*;
/**
*
* @author Administrator
*/
public class SocketReceive extends Thread{
Socket socket;
BufferedReader bufferedReader;
/** Creates a new instance of SocketReceive */
public SocketReceive() {
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
SocketReceive sr=new SocketReceive();
sr.receive();
}

public synchronized void receive(){
try{
socket=new Socket("192.168.0.102",7086);
bufferedReader=new BufferedReader(new InputStreamReader(socket.getInputStream()));
start();
}catch(Exception ex){
ex.printStackTrace();
}
}

public void run(){
try{
while(true){
System.out.println(bufferedReader.readLine());
sleep(1000);
}
}catch(Exception ex){
ex.printStackTrace();
}
}
}
到底是什么原因造成的?
...全文
112 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
auron01 2004-08-25
  • 打赏
  • 举报
回复
import java.net.*;
import java.io.*;
/**
*
* @author Administrator
*/
public class SocketPost extends Thread{
ServerSocket serverSocket;
Socket socket;
OutputStream os;
DataOutputStream dos;
PrintStream ps;
Thread thread;
/** Creates a new instance of SocketPost */
public SocketPost() {
}

public void post(){
try{
serverSocket=new ServerSocket(7086);
while(true){
socket=serverSocket.accept();
if(socket!=null){
thread=new Thread(this);
thread.start();
}
}
}catch(Exception ex){
//System.out.println("abc");
//System.out.println(ex.getMessage());
ex.printStackTrace();
}
}

public void run(){
try{
os=socket.getOutputStream();
ps=new PrintStream(os);
while(true){
ps.println((int)Math.round(Math.random()*100));
//System.out.println((int)Math.round(Math.random()*100));
thread.sleep(1000);
}
}catch(Exception ex){
//System.out.println("bcd");
ex.printStackTrace();
}
}
public static void main(String[] args) {
// TODO code application logic here
SocketPost sp=new SocketPost();
sp.post();
}
}
那我现在这样写对吗?
alpha15 2004-08-24
  • 打赏
  • 举报
回复
其实你只要在此加个死循环不断的监听客户端就可以了socket=serverSocket.accept();
tomcatjava 2004-08-24
  • 打赏
  • 举报
回复
忘记开启线程了:
Socket socket = null;
while( true ) {
socket = serverSocket.accept();
ProcessSocketThread socketThread = new ProcessSocketThread( socket );
socketThread.start();
}
tomcatjava 2004-08-24
  • 打赏
  • 举报
回复
应该这样:

Socket socket = null;
while( true ) {
socket = serverSocket.accept();
ProcessSocketThread socketThread = new ProcessSocketThread( socket );
}

public class ProcessSocketThread extends Thread {
private Socket socket = null;

public ProcessSocket( Socket socket ) {
this.socket = socket;
}

public void run() {
//do anything you wanna
}
}
westwin 2004-08-24
  • 打赏
  • 举报
回复
socket=serverSocket.accept();
这句话就执行一次,不是在一直监听client的连接,所以当client断开连接时,你的程序就不能读取数据了.

这种简单的b/s结构,一般都是Server中一个循环while(true){socket=serverSocket.accept();//如果有client连接请求,那么给该client重新启动一个thread,处理client的请求}

auron01 2004-08-24
  • 打赏
  • 举报
回复
顶,这里没人了吗?
auron01 2004-08-24
  • 打赏
  • 举报
回复
分少可以再加,帮我看看阿

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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