Before marking this as a possible duplicate, please give it a read once as I couldn't find any answers. I am learning Socket programming and creating a very basic server-client file transfer program using sockets. Here's my server module:
import java.io.*;
import java.net.*;
import static java.lang.System.out;
public class ServerMain {
public static void main(String[] args) {
int PORT=13267;
ServerSocket servSocket;
Socket client;
File source=new File("E:/Styles of Beyond.wav");;
BufferedInputStream bis;
BufferedReader br;
String s="";
InputStream is;
byte[] byteBuffer=new byte[10240];
OutputStream os;
int count=0;
try{
servSocket=new ServerSocket(PORT);
out.println("Server running on port: "+PORT+", waiting for client...");
client=servSocket.accept();
client.setKeepAlive(true);
out.println("Client: "+client.getRemoteSocketAddress()+" connected. Sending file...");
bis=new BufferedInputStream(new FileInputStream(source));
os=client.getOutputStream();
is=client.getInputStream();
while((count=bis.read(byteBuffer))>0){
os.write(byteBuffer, 0, count);
}
out.println("Waiting for confirmation from client...");
br=new BufferedReader(new InputStreamReader(is));
if(br.readLine()=="Done")
out.println("Done.");
else
out.println("Couldn't get confirmation from server.");
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
And the client module:
import static java.lang.System.out;
import java.io.*;
import java.net.*;
public class ClientMain {
public static void main(String[] args) {
int SERV_PORT=13267;
Socket server;
File destination=new File("E:/copy.wav");
byte[] byteBuffer=new byte[10240];
InputStream is;
OutputStream os;
BufferedWriter bw;
int count=0;
try{
out.println("Tyring to connect to server...");
server=new Socket("localhost",SERV_PORT);
out.println("Connected to server: "+server.getRemoteSocketAddress());
is=server.getInputStream();
os=server.getOutputStream();
out.println("Receiving data...");
BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(destination));
while((count=is.read(byteBuffer))>0){
bos.write(byteBuffer, 0, count);
}
bw=new BufferedWriter(new OutputStreamWriter(os));
bw.write("Done");
bw.newLine();
out.println("Data successfully written to copy.wav");
}
catch(Exception e){
e.printStackTrace();
}
}
}
The code works and even the file copy.wav is created properly, but then after writing on os of client, server gets stuck on "Waiting for confirmation from client...", and client is stuck on "Receiving data...". I tried using PrintWriter on client side instead of BufferedWriter, and also simply tried to write a single byte on os, and checking that byte for file received-confirmation on server side, but nothing seems to work, the client is not sending anything. One thing I noticed is that calling is.ready() on server returns false always.
If I remove all the code related to sending and receiving confirmation of file on both client & server, the client program produces SocketException: connection reset, as now the server doesn't wait for the client to complete its is.read() task and closes connection as soon as it completes writing using os.write(), which creates copy.wav with partial data of lesser size than original. How do I do this thing properly?
P.S: I know I haven't closed streams and handled exceptions properly but that doesn't seem to be a problem here.