How to read and write data from socket in java
Written by Ankur Goel
ServerSocket serverSocket=new ServerSocket(port);
// The below statement will wait for the connection from the other machine at the given port and as soon as the connection is established a socket is allocated for the connection and
public Socket NewSocket= serverSocket.accept();
//for reading the data from the socket
BufferedInputStream bufferinput=new BufferedInputStream(NewSocket.getInputStream());
DataInputStream datainput=new DataInputStream(bufferinput);
byte[] handsize=new byte[32];
// The control will halt at the below statement till all the 32 bytes are not read from the socket.
datainput.readFully(handsize);
// For writing the data on the socket
Socket socket=null;
socket=new Socket(HostName,port);
BufferedOutputStream Boutput=new BufferedOutputStream(socket.getOutputStream());
byte[] headermake=new byte[32];
Boutput.write(headermake);
Boutput.flush();
















Comments
http://www.espionagem.esp.br/caneta-espia
http://www.espionagem.esp.br/caneta-espia