Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java SE 综合讨论区  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 高手指教!关于java编写的服务器程序中的问题!
jintao30371253





发贴: 2
积分: 0
于 2005-05-18 18:56 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
我今天搞了半天,也没搞定,这个小问题,请大师指导!谢谢了
import java.io.*;
import java.net.*;
public class Server extends Thread
{
public static final int Default_Port=6543;
protected int port;
//类port可以被这个类本身、它的子类以及同一个包中的所有其它类访问
protected ServerSockt listen_socket;
public static void fail(Exception e, String msg)
//定义出错例程:如果出现异常错误,退出程序。
{
System.err.println(msg + ": " + e);
System.exit(1);
}
// 定义并启动服务器的Socket 例程,监听客户机的连接请求。
public Server(int port)
{
if(port == 0) port = Default_Port;
this.port = port;
try
{
listen_socket = new ServerSocket(port);
}
catch(IOException e)
{
fail(e, "Exception creating server socket");
System.out.println("Server: listening on port" + port);
this.start();
}
/* 下面为服务器监听线程的主程序,该线程一直循环执行,监听并接受客户机发出的连接
请求,对每一个连接,均产生一个连接对象与之对应,通过Socket 通道进行通信.*/
public void run()
{
try
{
while(true)
{
Socket client_socket = listen_socket.accept();
Connection c = new Connection(client_socket);
}
}
catch(IOException e) fail(e,"Exception while listening for connections");
}
// 启动服务器主程序
public static void main(String args[])
{
int port = 0;
if (args.length == 1)
{
try {
port = Integer.parseInt(args[0]);
catch(NumberFormatException e) port = 0;
}
new Server(port);
}// End of the main
} ; // End of Server class
//以下定义了Connection 类,它是用来处理与客户机的所有通信的线程。
class Connection extends Thread
{
protected Socket client;
protected DataInputStream in;
protected PrintStream out;
// 初始化通信流并启动线程
public Connection(Socket client_socket)
{
client = client_socket;
try
{
in = new DataInputStream(client.getinputStream());
out = new PrintStream(client.getOutputStream());
}
catch(IOException e)
{
try {client.close();}
catch(IOException e2){
System.err.println("Exception while getting socket streram: " + e);}
Return();
}
this.start();
} // End of Connection method
// 服务例程:读出一行文本;反转文本;返回文本。
public void run()
{
String line;
StringBuffer revline;
int len;
try
{
for(;;)
line = in.readline(); // Read a line
if(line == null) break;
len = line.length(); // Reverse the line
revline = new StringBuffer(len);
for(int i = len-1; i >=0; i--)
revline.insert(len-1-I,line.charAt(i));
out.println(revline); // Write out the reverse line
}
catch(IOException e){}
finally
{
try
{
client.close();
}
catch(IOException e2){}
}
// End of run method
}
}
}
}
// End of Connection class
出现的错误是:
Server.java:32: illegal start of expression
public void run()
^
1 error


why edited on 2005-05-20 06:12


话题树型展开
人气 标题 作者 字数 发贴时间
4573 高手指教!关于java编写的服务器程序中的问题! jintao30371253 2610 2005-05-18 18:56
3243 Re:高手指教!关于java编写的服务器程序中的问题! danzel 13 2005-05-19 16:59
3356 Re:高手指教!关于java编写的服务器程序中的问题! why 71 2005-05-20 06:19

flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923