网络开发Socket和ServerSocket

2022-04-29 09:15:45 浏览数 (1)

Socket和ServerSocket

Socket为“孔”或“插座”,创建Socket,打开连接Socket的输入或输出流,对Socket进行读写,关闭Socket。

Accept方法用于产生“阻塞”,这里有getInputStream方法和getOutInputStream方法,会产生一个IOException,

在Java.net包中,有Socket和ServerSocket两个类。以JDK1.6介绍:

构造方法
代码语言:javascript复制
public Socket()
public Socket(String host, int port)
//host - 主机名,或者为 null,表示回送地址
//port - 端口号

public Socket(InetAddress address,int port)
//address - IP 地址
//port - 端口号

ServerSocket类有3个构造方法

代码语言:javascript复制
ServerSocket(int port)

ServerSocket(int port,int backlog)

ServerSocket(int port,int backlog,InetAddress binAddr)
服务器与客户端通信
代码语言:javascript复制
package two;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;

public class ServerSocket1 {
 public static void main(String[] args) {
   // TODO Auto-generated method stub
   try {
     ServerSocket ss = new ServerSocket(2007);
     while(true) {
       Socket s = ss.accept();
       InputStream is = s.getInputStream();
       OutputStream os = s.getOutputStream();
       PrintStream ps = new PrintStream(os);
       ps.println("helloworld, i am server thinkpad");
       
       DataInputStream dis = new DataInputStream(is);
       String str = dis.readLine();
       System.out.println(str);
       s.close();
       
     }
   }
   catch(IOException ee) {
     System.out.println(ee);
   }
   catch(Exception e) {
     System.out.println(e);
   }
 }
}
代码语言:javascript复制
package two;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.ConnectException;
import java.net.Socket;

public class ClientSocket {
 public static void main(String[] args) {
   // TODO Auto-generated method stub
   try {
     Socket s = new Socket("########",2007);
     InputStream is = s.getInputStream();
     OutputStream os = s.getOutputStream();
     PrintStream ps = new PrintStream(os);
     ps.println("hello , i am client");
     
     DataInputStream dis = new DataInputStream(is);
     
     String str = dis.readLine();
     System.out.println(str);
     s.close();
     
   }
   catch(ConnectException eee) {
     System.out.println(eee);
   }
   catch(IOException ee) {
     System.out.println(ee);
   }
   catch(Exception e) {
     System.out.println(e);
   }
 }
}
线程:

线程也称为轻量级进程,是程序执行的最小单元。是程序中一个单一的顺序控制流程,在程序中同时运行多个线程,称为多线程。进程是一个可执行的程序。一个应用可以有一个或多个进程,对于一个进程可以有一个或多个线程,其中一个是主线程,public static void main。

创建,执行线程的速度比进程更快。

多客户请求服务
代码语言:javascript复制
public class DuoServer{
public static void main(String[] args){
 boolean connected = true;
 try{
  ServerSocket ss = new ServerSocket(2007);
  int clientnum=0;
  while(connected){
   ServerThread st = new ServerThread(ss.accept(),clientnum);
   st.start();
   clientnum  ;
   System.out.println(clientnum);
  }
}
catch(Excepiton e){
 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();
  PrintStream ps = new PrintStream(os);
  ps.println("i am server");
  
  DataInputStream dis = new DataInputStream(is);
  String str = dis.readLine();
  System.out.println(str);
  }
  catch(Excepiton e){
   System.out.println(e);
  }
 }
}

0 人点赞