Rahul/socket/read/pr4 (#6575)
* Making examples simple * Changing variable names * Modificatons in naming * Review changes
This commit is contained in:
parent
4151e55d3b
commit
cc73a3051b
@ -1,35 +1,27 @@
|
|||||||
package com.baeldung.socket.read;
|
package com.baeldung.socket.read;
|
||||||
|
|
||||||
import java.net.*;
|
import java.net.*;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
|
|
||||||
public class Client {
|
public class Client {
|
||||||
|
|
||||||
//Initialize socket, input and output stream
|
|
||||||
private Socket socket = null;
|
|
||||||
private DataInputStream in = null;
|
|
||||||
private DataOutputStream out = null;
|
|
||||||
|
|
||||||
public void runClient(String ip, int port) {
|
public void runClient(String ip, int port) {
|
||||||
try {
|
try {
|
||||||
socket = new Socket(ip, port);
|
Socket socket = new Socket(ip, port);
|
||||||
System.out.println("Connected to server ...");
|
System.out.println("Connected to server ...");
|
||||||
in = new DataInputStream(System.in);
|
DataInputStream in = new DataInputStream(System.in);
|
||||||
out = new DataOutputStream(socket.getOutputStream());
|
DataOutputStream out = new DataOutputStream(socket.getOutputStream());
|
||||||
} catch(Exception e) {
|
|
||||||
e.printStackTrace();
|
char type = 's'; // s for string
|
||||||
}
|
int length = 29;
|
||||||
char type = 's'; // s for string
|
String data = "This is a string of length 29";
|
||||||
int length = 29;
|
byte[] dataInBytes = data.getBytes(StandardCharsets.UTF_8);
|
||||||
String data = "This is a string of length 29";
|
//Sending data in TLV format
|
||||||
byte[] dataInBytes = data.getBytes();
|
|
||||||
//Sending data in TLV format
|
|
||||||
try {
|
|
||||||
out.writeChar(type);
|
out.writeChar(type);
|
||||||
out.writeInt(length);
|
out.writeInt(length);
|
||||||
out.write(dataInBytes);
|
out.write(dataInBytes);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
// TODO Auto-generated catch block
|
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,48 +1,46 @@
|
|||||||
package com.baeldung.socket.read;
|
package com.baeldung.socket.read;
|
||||||
|
|
||||||
import java.net.*;
|
import java.net.*;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
|
|
||||||
public class Server {
|
public class Server {
|
||||||
|
|
||||||
//Socket and input stream
|
|
||||||
private Socket socket = null;
|
|
||||||
private ServerSocket server = null;
|
|
||||||
private DataInputStream in = null;
|
|
||||||
|
|
||||||
public void runServer(int port) {
|
public void runServer(int port) {
|
||||||
//Start the server and wait for connection
|
//Start the server and wait for connection
|
||||||
try {
|
try {
|
||||||
server = new ServerSocket(port);
|
ServerSocket server = new ServerSocket(port);
|
||||||
System.out.println("Server Started. Waiting for connection ...");
|
System.out.println("Server Started. Waiting for connection ...");
|
||||||
socket = server.accept();
|
Socket socket = server.accept();
|
||||||
System.out.println("Got connection from client.");
|
System.out.println("Got connection from client.");
|
||||||
//Get input stream from socket variable and convert the same to DataInputStream
|
//Get input stream from socket variable and convert the same to DataInputStream
|
||||||
in = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
|
DataInputStream in = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
|
||||||
//Read type and length of data
|
//Read type and length of data
|
||||||
char dataType = in.readChar();
|
char dataType = in.readChar();
|
||||||
int length = in.readInt();
|
int length = in.readInt();
|
||||||
System.out.println("Type : "+dataType);
|
System.out.println("Type : "+dataType);
|
||||||
System.out.println("Lenght :"+length);
|
System.out.println("Lenght :"+length);
|
||||||
//Read String data in bytes
|
if(dataType == 's') {
|
||||||
byte[] messageByte = new byte[length];
|
//Read String data in bytes
|
||||||
boolean end = false;
|
byte[] messageByte = new byte[length];
|
||||||
String dataString = "";
|
boolean end = false;
|
||||||
int totalBytesRead = 0;
|
StringBuilder dataString = new StringBuilder(length);
|
||||||
//We need to run while loop, to read all data in that stream
|
int totalBytesRead = 0;
|
||||||
while(!end) {
|
//We need to run while loop, to read all data in that stream
|
||||||
int currentBytesRead = in.read(messageByte);
|
while(!end) {
|
||||||
totalBytesRead = currentBytesRead + totalBytesRead;
|
int currentBytesRead = in.read(messageByte);
|
||||||
if(totalBytesRead <= length) {
|
totalBytesRead = currentBytesRead + totalBytesRead;
|
||||||
dataString += new String(messageByte,0,currentBytesRead);
|
if(totalBytesRead <= length) {
|
||||||
} else {
|
dataString.append(new String(messageByte,0,currentBytesRead,StandardCharsets.UTF_8));
|
||||||
dataString += new String(messageByte,0,length - totalBytesRead + currentBytesRead);
|
} else {
|
||||||
}
|
dataString.append(new String(messageByte,0,length - totalBytesRead + currentBytesRead,StandardCharsets.UTF_8));
|
||||||
if(dataString.length()>=length) {
|
}
|
||||||
end = true;
|
if(dataString.length()>=length) {
|
||||||
|
end = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
System.out.println("Read "+length+" bytes of message from client. Message = "+dataString);
|
||||||
}
|
}
|
||||||
System.out.println("Read "+length+" bytes of message from client. Message = "+dataString);;
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user