I am trying to send set of files through socket C# and Java application, I am not able to recieve all files. The server application closes connections, I think, because the server always closes connection after sending the file without making sure that the client app received the file.
Here is the C# client code:
public static void connectWithRemoteServer(string strIp, string strMessage, out
string strResult)
{
strResult = "";
TcpClient tcpClient = new TcpClient();
tcpClient.Connect(strIp, 1593);
NetworkStream networkStream = tcpClient.GetStream();
StreamReader sr = new StreamReader(networkStream);
if (strMessage.Substring(0,1).Equals("s"))
{
byte[] byteBuffer = Encoding.ASCII.GetBytes(strMessage);
networkStream.Write(byteBuffer, 0, byteBuffer.Length);
strResult = sr.ReadLine();
}
else if (strMessage.Substring(0, 1).Equals("f"))
{
byte[] byteBuffer = Encoding.ASCII.GetBytes(strMessage);
networkStream.Write(byteBuffer, 0, byteBuffer.Length);
int length = int.Parse(sr.ReadLine());
byte[] buffer = new byte[length];
networkStream.Read(buffer, 0, (int)length);
string var = strMessage.Substring(1);
//write to file
BinaryWriter bWrite = new BinaryWriter(File.Open(var, FileMode.Create));
bWrite.Write(buffer);
bWrite.Flush();
bWrite.Close();
strResult = "ok";
}
tcpClient.Close();
}
}
foreach (string strFilePath in lst)
{
Utilities.connectWithRemoteServer("192.168.0.167", "f" + strFilePath, out str);
}
Java Server app
while(true)
{
String listeFiles = "";
ServerSocket serverSocket = new ServerSocket(1593);
System.out.println("Server started and waiting for request..");
Socket socket = serverSocket.accept();
System.out.println("Connection accepted from " + socket);
// this is to send to the client application
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
// this is to receive from the client application
BufferedInputStream in = new BufferedInputStream(socket.getInputStream());
byte[] bytearray = new byte[in.available()];// recived byte from the
// client application
in.read(bytearray, 0, bytearray.length);// read recieved byte from
String strRecieved = new String(bytearray);// get the received string
System.out.println(strRecieved);
//strRecieved = strRecieved.trim();
if(strRecieved.substring(0,1).equals("s"))
{
List<String> lstFiles = GetFiles.getListFilesPath("./testData");
for (String str : lstFiles) {
listeFiles = str + "+" + listeFiles;
}
String listFilesToSend = listeFiles.substring(0, listeFiles.length() - 1);
out.println(listFilesToSend);
System.out.println("List Files Send ");
serverSocket.close();
socket.close();
}
else if (strRecieved.substring(0,1).equals("f"))
{
File file = new File(strRecieved.substring(1));
// // send file length
out.println(file.length());
// read file to buffer
byte[] buffer = new byte[(int) file.length()];
DataInputStream dis = new DataInputStream(new FileInputStream(file));
dis.read(buffer, 0, buffer.length);
// // send file
BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream());
bos.write(buffer);
bos.flush();
serverSocket.close();
socket.close();
}
}
}
How to make sure the file has been received successfully before send the next file Issue
unable to write data to the transport connection an existing connection was forcibly closed
Any help would be appreciated. Thank you .