实现书上的某个例子
客户端代码为
服务器的代码为
看到的首先是输出null 接着就报错 说拒绝访问异常java.net.ConnectException: Connection refused: connect
是端口问题 还是什么呢?
客户端代码为
import java.net.*;
import java.io.*;
public class KeSocket{
public static void main(String args[]) {
try {
Socket s=new Socket("127.0.0.1",8888);
InputStream is=s.getInputStream();
OutputStream os=s.getOutputStream();
PrintStream ps=new PrintStream(os);
ps.println("hello");
DataInputStream dis=new DataInputStream(is);
String request=dis.readLine();
System.out.println(request);
s.close();
}
catch(ConnectException e){
System.out.println("异常"+e);
}
catch (IOException ee) {
// TODO: handle exception
System.out.println("异常"+ee);
}
catch (Exception eee) {
// TODO: handle exception
System.out.println("异常"+eee);
}
}
}
服务器的代码为
import java.net.*;
import java.io.*;
import com.sun.org.apache.bcel.internal.generic.AALOAD;
public class DuoServer{
public static void main(String args[]){
boolean connected=true;
try{
ServerSocket ss=new ServerSocket(8888);
int clientnum=0;
while(connected){
ServerThread st=new ServerThread(ss.accept(),clientnum);
st.start();
clientnum++;
System.out.println(clientnum);
}
}
catch (Exception e) {
// TODO: handle exception
System.out.println("异常"+e);
}
}
}
class ServerThread extends Thread{
private Socket s;
int clientnum;
public ServerThread(Socket s,int num)
{
this.s=s;
clientnum=num+1;
}
public void run()
{
try {
InputStream is=s.getInputStream();
OutputStream os=s.getOutputStream();
DataInputStream dis=new DataInputStream(is);
String request=dis.readLine();
System.out.println(request);
PrintStream ps=new PrintStream(os);
ps.println("Bye");
s.close();
} catch (Exception e) {
// TODO: handle exception
System.out.println("异常"+e);
}
}
}
看到的首先是输出null 接着就报错 说拒绝访问异常java.net.ConnectException: Connection refused: connect
是端口问题 还是什么呢?