好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

java如何实现socket连接方法封装

Java实现socket连接技巧

Socket通信几乎无时不在,当然能够搜集到的信息也大量存在, 为了避免重复的劳作, 抽取了关于客户端和服务端的Socket, 并将其应用到适合JVM(LInux/Windows)或者DVM(Android)平台。

这个封装好的API具有以下优势:

1.满足具有Socket客户端需求的基本应用。

2.满足具有Socket服务端的基本应用。具备并发能力, 能满足可设定个数客户端连接。

本文的目的就是为了对Socket做一个封装, 方便客户端和服务端能直接使用Socket.封装好的API可以从下面获取

Java Socket的封装

其中src/中的是API源码; usage/目录是使用例程

1 客户端Socket API要点

1)客户端和指定的服务端相连, 因此客户端需要指明服务端对应的IP地址和端口号

2)需要设置超时返回

3)需要设置循环等待, 因为基本的Socket通信都是一来一回, 这种来回是通过阻塞来完成的。

4)每个客户端连入服务端的时候, 都具备本身的ID, 类似于HTTP的Session, 这点容易被忽视。在多客户端连接中, 可以重点关注。本文提供的代码也有所提及, 但没有深入, 这点留给读者进一步发掘。

代码参照/usage目录下的客户端测试代码, 注意, 先启动服务端,或者你拿着NetAssis 来测试也不错.

2 服务端Socket API要点

1)服务端一般是被多个客户端连接的, 并且这些连接要求服务端做相似的处理, 因此这里就将这些相似处理, 抽象成一个SingleTask.java 接口, 具体的业务只需要实现这样的接口, 就可以并行的处理这些Task.

2)不能无限制的让客户端连入Server, 因此需要设置上限值

3)启动线程池, 每个线程针对一个具体的客户端连接

4)注意接收阻塞位置, 需要设置死循环, 读不到数据将死守着等待(但别耽误其它线程处理事情)

5)注意服务端要在死循环中侦听, 这样保证不错过任何来自客户端的请求。

代码参照:/usage目录下的Server端测试代码。

代码中注释很多,因此这里就不详细述说。

常见问题

1、客户端Client的时候, 如果存在网络问题, 为了避免网络问题,造成客户端长时间等待, 此时要设置一个TimeOut

?

1

2

3

clientSocket = new Socket();

//这个TimeOut是连接等待时间

clientSocket.connect(tcpAddress, timeOut);

2、当客户端已经连接, 每次收到一个数据, 客户端将启动处理, 假如服务器长久不发数据, 此时客户端会阻塞等待, 为了避免这个时候的等待, 可以设置一个超时

?

1

clientSocket.setSoTimeout(timeOut);

Java使用socket实现一个多线程web服务器的方法

除了服务器类,还包括请求类和响应类

请求类:获取客户的HTTP请求,分析客户所需要的文件响应类:获得用户请求后将用户需要的文件读出,添加上HTTP应答头。发送给客户端。

服务器处理类

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

package com.lp.app.webserver;

import java.io.*;

import java.net.*;

//使用Socket创建一个WEB服务器,本程序是多线程系统以提高反应速度。

class WebServer

{

public static String WEBROOT = "" ; //默认目录

  public static String defaultPage = "index.htm" ; //默认文件

  public static void main (String [] args) throws IOException

  {

  System.out.println ( "服务器启动...\n" );

   //使用8080端口提供服务

  ServerSocket server = new ServerSocket ( 8080 );

  while ( true )

  {

   //阻塞,直到有客户连接

   Socket sk = server.accept ();

   System.out.println ( "Accepting Connection...\n" );

   //启动服务线程

   new WebThread (sk).start ();

  }

  }

}

//使用线程,为多个客户端服务

class WebThread extends Thread

{

  private Socket sk;

  WebThread (Socket sk)

  {

   this .sk = sk;

  }

  //线程体

  public void run ()

  {

   InputStream in = null ;

   OutputStream out = null ;

   try {

   in = sk.getInputStream();

   out = sk.getOutputStream();

   //接收来自客户端的请求。

   Request rq = new Request(in);

   //解析客户请求

   String sURL = rq.parse();

   System.out.println( "sURL=" +sURL);

   if (sURL.equals( "/" ))

     sURL = WebServer.defaultPage;

   Response rp = new Response(out);

   rp.Send(sURL);

    }

   catch (IOException e)

   {

   System.out.println (e.toString ());

   }

   finally

   {

   System.out.println ( "关闭连接...\n" );

   //最后释放资源

   try {

    if (in != null )

    in.close ();

    if (out != null )

    out.close ();

    if (sk != null )

    sk.close ();

   }

   catch (IOException e)

   {

   }

   }

  }

}

请求类

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

package com.lp.app.webserver;

import java.io.*;

import java.net.*;

//获取客户的HTTP请求,分析客户所需要的文件

public class Request{

  InputStream in = null ;

  //获得输入流。这是客户的请求数据。

  public Request(InputStream input){

  this .in = input;

  }

  //解析客户的请求

  public String parse() {

  //从Socket读取一组数据

  StringBuffer requestStr = new StringBuffer( 2048 );

  int i;

  byte [] buffer = new byte [ 2048 ];

  try {

  i = in.read(buffer);

  }

  catch (IOException e) {

  e.printStackTrace();

  i = - 1 ;

  }

  for ( int j= 0 ; j<i; j++) {

  requestStr.append(( char ) buffer[j]);

  }

  System.out.print(requestStr.toString());

  return getUri(requestStr.toString());

  }

  //获取URI信息字符

  private String getUri(String requestString) {

  int index1, index2;

  index1 = requestString.indexOf( ' ' );

  if (index1 != - 1 ) {

  index2 = requestString.indexOf( ' ' , index1 + 1 );

  if (index2 > index1)

   return requestString.substring(index1 + 1 , index2);

  }

  return null ;

  }

}

响应类

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

package com.lp.app.webserver;

import java.io.*;

import java.net.*;

//获得用户请求后将用户需要的文件读出,添加上HTTP应答头。发送给客户端。

public class Response{

  OutputStream out = null ;

  //发送请求的文件 public void Send(String ref) throws IOException {

  byte [] bytes = new byte [ 2048 ];

  FileInputStream fis = null ;

  try {

  //构造文件

  File file = new File(WebServer.WEBROOT, ref);

  if (file.exists()) {

   //构造输入文件流

   fis = new FileInputStream(file);

   int ch = fis.read(bytes, 0 , 2048 );

   //读取文件

   String sBody = new String(bytes, 0 );

   //构造输出信息

   String sendMessage = "HTTP/1.1 200 OK\r\n" +

   "Content-Type: text/html\r\n" +

   "Content-Length: " +ch+ "\r\n" +

   "\r\n" +sBody;

   //输出文件

   out.write(sendMessage.getBytes());

  } else {

   // 找不到文件

   String errorMessage = "HTTP/1.1 404 File Not Found\r\n" +

   "Content-Type: text/html\r\n" +

   "Content-Length: 23\r\n" +

   "\r\n" +

   "<h1>File Not Found</h1>" ;

   out.write(errorMessage.getBytes());

  }

  }

  catch (Exception e) {

  // 如不能实例化File对象,抛出异常。

  System.out.println(e.toString() );

  }

  finally {

  if (fis != null )

   fis.close();

  }

  }

  //获取输出流

  public Response(OutputStream output) {

  this .out = output;

}

}

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

原文链接:https://blog.csdn.net/lemonrel/article/details/103189358

查看更多关于java如何实现socket连接方法封装的详细内容...

  阅读:25次