例子 测试成功
/**
@version 1.20 2004-08-03
@author Cay Horstmann
*/import java.io.*;
import java.net.*;
import java.util.*;/**
This program makes a socket connection to the atomic clock
in Boulder, Colorado, and prints the time that the
server sends.
*/
public class SocketTest
{
public static void main(String[] args)
{
try
{
Socket s = new Socket("127.0.0.1", 8189);
try
{
InputStream inStream = s.getInputStream();
Scanner in = new Scanner(inStream);
while (in.hasNextLine())
{
String line = in.nextLine();
System.out.println(line);
}
}
finally
{
s.close();
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
/**
@author Cay Horstmann
@version 1.20 2004-08-03
*/import java.io.*;
import java.net.*;
import java.util.*;/**
This program implements a multithreaded server that listens to port 8189 and echoes back
all client input.
*/
public class ThreadedEchoServer
{
public static void main(String[] args )
{
try
{
int i = 1;
ServerSocket s = new ServerSocket(8189); while (true)
{
Socket incoming = s.accept();
System.out.println("Spawning " + i);
Runnable r = new ThreadedEchoHandler(incoming, i);
Thread t = new Thread(r);
t.start();
i++;
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
}/**
This class handles the client input for one server socket connection.
*/
class ThreadedEchoHandler implements Runnable
{
/**
Constructs a handler.
@param i the incoming socket
@param c the counter for the handlers (used in prompts)
*/
public ThreadedEchoHandler(Socket i, int c)
{
incoming = i; counter = c;
} public void run()
{
try
{
try
{
InputStream inStream = incoming.getInputStream();
OutputStream outStream = incoming.getOutputStream();
Scanner in = new Scanner(inStream);
PrintWriter out = new PrintWriter(outStream, true /* autoFlush */);
out.println( "Hello! Enter BYE to exit." );
// echo client input
boolean done = false;
while (!done && in.hasNextLine())
{
String line = in.nextLine();
out.println("Echo: " + line);
if (line.trim().equals("BYE"))
done = true;
}
}
finally
{
incoming.close();
}
}
catch (IOException e)
{
e.printStackTrace();
}
} private Socket incoming;
private int counter;
}
/**
@version 1.20 2004-08-03
@author Cay Horstmann
*/import java.io.*;
import java.net.*;
import java.util.*;/**
This program makes a socket connection to the atomic clock
in Boulder, Colorado, and prints the time that the
server sends.
*/
public class SocketTest
{
public static void main(String[] args)
{
try
{
Socket s = new Socket("127.0.0.1", 8189);
try
{
InputStream inStream = s.getInputStream();
Scanner in = new Scanner(inStream);
while (in.hasNextLine())
{
String line = in.nextLine();
System.out.println(line);
}
}
finally
{
s.close();
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
/**
@author Cay Horstmann
@version 1.20 2004-08-03
*/import java.io.*;
import java.net.*;
import java.util.*;/**
This program implements a multithreaded server that listens to port 8189 and echoes back
all client input.
*/
public class ThreadedEchoServer
{
public static void main(String[] args )
{
try
{
int i = 1;
ServerSocket s = new ServerSocket(8189); while (true)
{
Socket incoming = s.accept();
System.out.println("Spawning " + i);
Runnable r = new ThreadedEchoHandler(incoming, i);
Thread t = new Thread(r);
t.start();
i++;
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
}/**
This class handles the client input for one server socket connection.
*/
class ThreadedEchoHandler implements Runnable
{
/**
Constructs a handler.
@param i the incoming socket
@param c the counter for the handlers (used in prompts)
*/
public ThreadedEchoHandler(Socket i, int c)
{
incoming = i; counter = c;
} public void run()
{
try
{
try
{
InputStream inStream = incoming.getInputStream();
OutputStream outStream = incoming.getOutputStream();
Scanner in = new Scanner(inStream);
PrintWriter out = new PrintWriter(outStream, true /* autoFlush */);
out.println( "Hello! Enter BYE to exit." );
// echo client input
boolean done = false;
while (!done && in.hasNextLine())
{
String line = in.nextLine();
out.println("Echo: " + line);
if (line.trim().equals("BYE"))
done = true;
}
}
finally
{
incoming.close();
}
}
catch (IOException e)
{
e.printStackTrace();
}
} private Socket incoming;
private int counter;
}
写的就是多线程 再就是连接不上不止跟你的程序有关,还跟你的端口是否打开有关
那个服务器端和客户端不在一起的,是不是用我自己的客户端就行了呀,
大哥的这个服务器端写的很周密吗.呆小弟看有些难..
给小弟简单讲讲每个文件在程序中起的作用吧.(简单讲讲就成,小弟在自己去苦读大哥代码)
问完这个,小弟马上结帖给分.要不求了半天又看不明白...
大哥的:
AddUser.java
UserInfo.java
CommonMethod.java
ChatServer.java
AcceptParPro.java