由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Java版 - 新手问题
相关主题
socket programing server-server怎么通讯?新手求教 BufferedReader.readLine()
Socket Connection refused in high-loaded serversSocket created/called in Servlet timed out
请问一下能这样编网站吗?
新手问一个多线程的问题A Simple Java Socket Issue
Re: Client-Server and actionPerformedwhat is your opinion in this case?
如何让两个socket并行执行thread傻问题,关于java里的资源释放
[合集] 有点糊涂了,一个程序能不能开多个server socket服务?socket and simple web browser
弱问socketNo decent way for input password from command line.
相关话题的讨论汇总
话题: socket话题: import话题: client
进入Java版参与讨论
1 (共1页)
e******t
发帖数: 157
1
LZ看的是ORACLE网站上的教学材料. 在package部分有个例子. LZ吭吃吭吃该了
CLASSPATH什么的终于编译通过了, 试着运行了一下, SERVER可以运行, CLIENT说没有
MAIN什么的, 牛牛们帮忙看一下, 这程序能运行吗?
1)server.java
package mygame.server;
import java.io.*;
import java.net.*;
import mygame.client.Client;
import mygame.shared.Utilities;
public class Server {
public static void main(String args[]) {
ServerSocket serverSocket = null;
Utilities.printMsg("creating server socket");

try {
serverSocket = new ServerSocket(4444);
} catch (IOException e) {
System.err.println("Unable to create server socket, " + e);
System.exit(1);
}
Utilities.printMsg("accepting client connections");
while (true) {
try {
Socket clientSocket = serverSocket.accept();
new Client(clientSocket).start();
} catch (IOException e) {
System.err.println("Unable to accept socket connection, " + e);
System.exit(1);
}
}
}
}
2)client.java
package mygame.client;
import java.io.*;
import java.net.*;
import java.util.*;
import mygame.shared.Utilities;
public class Client extends Thread {
Socket clientSocket = null;
public Client(Socket s) {
clientSocket = s;
}
public void run() {
if (clientSocket == null) {
return;
}
PrintStream out = null;
Utilities.printMsg("creating output stream");
try {
out = new PrintStream(clientSocket.getOutputStream());
} catch (IOException e) {
System.err.println("Error binding output to socket, " + e);
System.exit(1);
}
Utilities.printMsg("writing current date");
Date d = new Date();
out.println(d);
try {
out.close();
clientSocket.close();
} catch (IOException e) {
}
}
protected void finalize() {
if (clientSocket != null) {
try {
clientSocket.close();
} catch (IOException e) {
}
clientSocket = null;
}
}
}
3)Utilities.java
package mygame.shared;
public class Utilities {
// set DEBUG = false and compile to stop debug messages
final static boolean DEBUG = true;
public static void printMsg(String msg) {
if (DEBUG) {
System.out.println(msg);
}
}
}
z****e
发帖数: 54598
2
main在server这个类里面
然后这个main里面把server和client都跑了
e******t
发帖数: 157
3
那您看这个例子, 是不是就不能象一般的SERVER/CLIENT那样先运行SERVER, 然后再运
行CLIENT那样给它跑起来?

【在 z****e 的大作中提到】
: main在server这个类里面
: 然后这个main里面把server和client都跑了

z****e
发帖数: 54598
4
可以是可以
不过你要改代码
把client部分的代码写到另外一个main里面去

【在 e******t 的大作中提到】
: 那您看这个例子, 是不是就不能象一般的SERVER/CLIENT那样先运行SERVER, 然后再运
: 行CLIENT那样给它跑起来?

e******t
发帖数: 157
5
o 那我可能得学一会儿再改 谢谢啊

【在 z****e 的大作中提到】
: 可以是可以
: 不过你要改代码
: 把client部分的代码写到另外一个main里面去

1 (共1页)
进入Java版参与讨论
相关主题
No decent way for input password from command line.Re: Client-Server and actionPerformed
eclipse debug function question如何让两个socket并行执行thread
新手请教一个问题[合集] 有点糊涂了,一个程序能不能开多个server socket服务?
java,多层map应该怎么写?求个葫芦弱问socket
socket programing server-server怎么通讯?新手求教 BufferedReader.readLine()
Socket Connection refused in high-loaded serversSocket created/called in Servlet timed out
请问一下能这样编网站吗?
新手问一个多线程的问题A Simple Java Socket Issue
相关话题的讨论汇总
话题: socket话题: import话题: client