File tree Expand file tree Collapse file tree
core-java-modules/core-java-exceptions-2/src
main/java/com/baeldung/socketexception
test/java/com/baeldung/socketexception Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package com .baeldung .socketexception ;
2+
3+ import java .io .BufferedReader ;
4+ import java .io .IOException ;
5+ import java .io .InputStreamReader ;
6+ import java .io .PrintWriter ;
7+ import java .net .Socket ;
8+
9+ public class SocketClient {
10+
11+ private Socket clientSocket ;
12+ private PrintWriter out ;
13+ private BufferedReader in ;
14+
15+ public void startConnection (String ip , int port ) throws IOException {
16+ clientSocket = new Socket (ip , port );
17+ out = new PrintWriter (clientSocket .getOutputStream (), true );
18+ in = new BufferedReader (new InputStreamReader (clientSocket .getInputStream ()));
19+ }
20+
21+ public String sendMessage (String msg ) throws IOException {
22+ out .println (msg );
23+ return in .readLine ();
24+ }
25+
26+ public void stopConnection () throws IOException {
27+ in .close ();
28+ out .close ();
29+ clientSocket .close ();
30+ }
31+
32+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .socketexception ;
2+
3+ import java .io .BufferedReader ;
4+ import java .io .IOException ;
5+ import java .io .InputStreamReader ;
6+ import java .io .PrintWriter ;
7+ import java .net .ServerSocket ;
8+ import java .net .Socket ;
9+
10+ public class SocketServer {
11+
12+ private ServerSocket serverSocket ;
13+ private Socket clientSocket ;
14+ private PrintWriter out ;
15+ private BufferedReader in ;
16+
17+ public void start (int port ) {
18+ try {
19+ serverSocket = new ServerSocket (port );
20+ clientSocket = serverSocket .accept ();
21+ out = new PrintWriter (clientSocket .getOutputStream (), true );
22+ in = new BufferedReader (new InputStreamReader (clientSocket .getInputStream ()));
23+ String msg = in .readLine ();
24+ if (msg .contains ("hi" ))
25+ out .println ("hi" );
26+ else
27+ out .println ("didn't understand" );
28+ close ();
29+ stop ();
30+ } catch (IOException e ) {
31+
32+ }
33+ }
34+
35+ private void close () throws IOException {
36+ in .close ();
37+ out .close ();
38+ }
39+
40+ private void stop () throws IOException {
41+ clientSocket .close ();
42+ serverSocket .close ();
43+ }
44+
45+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .socketexception ;
2+
3+ import java .io .IOException ;
4+ import java .net .SocketException ;
5+ import java .util .concurrent .Executors ;
6+
7+ import org .junit .BeforeClass ;
8+ import org .junit .Test ;
9+
10+ public class SocketExceptionHandlingUnitTest {
11+
12+ @ BeforeClass
13+ public static void runServer () throws IOException , InterruptedException {
14+ Executors .newSingleThreadExecutor ()
15+ .submit (() -> new SocketServer ().start (6699 ));
16+ Thread .sleep (100 );
17+ }
18+
19+ @ Test
20+ public void givenRunningServer_whenConnectToClosedSocket_thenHandleException () throws IOException {
21+ SocketClient client = new SocketClient ();
22+ client .startConnection ("127.0.0.1" , 6699 );
23+ try {
24+ client .sendMessage ("hi" );
25+ client .sendMessage ("hi again" );
26+ } catch (SocketException e ) {
27+ client .stopConnection ();
28+ }
29+ }
30+
31+ }
You can’t perform that action at this time.
0 commit comments