Skip to content

Commit aaddee0

Browse files
rahusrivmaibin
authored andcommitted
Rahul/socket/read/pr2 (eugenp#6398)
* Making examples simple * Changing variable names * Modificatons in naming * Read all data from server * Adding seperate TestSocketRead class having live test * Adding test case * Changing test name to live tests
1 parent 92b0345 commit aaddee0

3 files changed

Lines changed: 125 additions & 0 deletions

File tree

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.baeldung.socket.read;
2+
3+
import java.net.*;
4+
import java.io.*;
5+
6+
public class Client {
7+
8+
//Initialize socket, input and output stream
9+
private Socket socket = null;
10+
private DataInputStream in = null;
11+
private DataOutputStream out = null;
12+
13+
public void runClient(String ip, int port) {
14+
try {
15+
socket = new Socket(ip, port);
16+
System.out.println("Connected to server ...");
17+
in = new DataInputStream(System.in);
18+
out = new DataOutputStream(socket.getOutputStream());
19+
} catch(Exception e) {
20+
e.printStackTrace();
21+
}
22+
char type = 's'; // s for string
23+
int length = 29;
24+
String data = "This is a string of length 29";
25+
byte[] dataInBytes = data.getBytes();
26+
//Sending data in TLV format
27+
try {
28+
out.writeChar(type);
29+
out.writeInt(length);
30+
out.write(dataInBytes);
31+
} catch (IOException e) {
32+
// TODO Auto-generated catch block
33+
e.printStackTrace();
34+
}
35+
}
36+
37+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.baeldung.socket.read;
2+
3+
import java.net.*;
4+
import java.io.*;
5+
6+
public class Server {
7+
8+
//Socket and input stream
9+
private Socket socket = null;
10+
private ServerSocket server = null;
11+
private DataInputStream in = null;
12+
13+
public void runServer(int port) {
14+
//Start the server and wait for connection
15+
try {
16+
server = new ServerSocket(port);
17+
System.out.println("Server Started. Waiting for connection ...");
18+
socket = server.accept();
19+
System.out.println("Got connection from client.");
20+
//Get input stream from socket variable and convert the same to DataInputStream
21+
in = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
22+
//Read type and length of data
23+
char dataType = in.readChar();
24+
int length = in.readInt();
25+
System.out.println("Type : "+dataType);
26+
System.out.println("Lenght :"+length);
27+
//Read String data in bytes
28+
byte[] messageByte = new byte[length];
29+
boolean end = false;
30+
String dataString = "";
31+
int totalBytesRead = 0;
32+
//We need to run while loop, to read all data in that stream
33+
while(!end) {
34+
int currentBytesRead = in.read(messageByte);
35+
totalBytesRead = currentBytesRead + totalBytesRead;
36+
if(totalBytesRead <= length) {
37+
dataString += new String(messageByte,0,currentBytesRead);
38+
} else {
39+
dataString += new String(messageByte,0,length - totalBytesRead + currentBytesRead);
40+
}
41+
if(dataString.length()>=length) {
42+
end = true;
43+
}
44+
}
45+
System.out.println("Read "+length+" bytes of message from client. Message = "+dataString);;
46+
} catch (Exception e) {
47+
e.printStackTrace();
48+
}
49+
}
50+
51+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.baeldung.socket.read;
2+
3+
import java.util.concurrent.TimeUnit;
4+
import org.junit.jupiter.api.Test;
5+
6+
public class SocketReadAllDataLiveTest {
7+
8+
@Test
9+
public void givenServerAndClient_whenClientSendsAndServerReceivesData_thenCorrect() {
10+
//Run server in new thread
11+
Runnable runnable1 = () -> { runServer(); };
12+
Thread thread1 = new Thread(runnable1);
13+
thread1.start();
14+
//Wait for 10 seconds
15+
try {
16+
TimeUnit.SECONDS.sleep(10);
17+
} catch (InterruptedException e) {
18+
e.printStackTrace();
19+
}
20+
//Run client in a new thread
21+
Runnable runnable2 = () -> { runClient(); };
22+
Thread thread2 = new Thread(runnable2);
23+
thread2.start();
24+
}
25+
26+
public static void runServer() {
27+
//Run Server
28+
Server server = new Server();
29+
server.runServer(5555);
30+
}
31+
32+
public static void runClient() {
33+
//Run Client
34+
Client client = new Client();
35+
client.runClient("127.0.0.1", 5555);
36+
}
37+
}

0 commit comments

Comments
 (0)