Hey all,
I am working on client server architecture and i am just beginner in this thing. Here my server is of C and client is of Java and i want to send a binary/database (.db)/image file of size around 10 - 20 MB from C server to the Java client. But data is lost while implementing the following code:
Server side C code is:
int sockfd, newsockfd, portno, clilen;
struct sockaddr_in serv_addr, client_addr;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if(sockfd < 0)
{
strcpy(message,"Error opening socket");
eFlag = 1;
send_message(message);
}
bzero((char *)&serv_addr, sizeof(serv_addr));
portno = 6789;
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
if(bind(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
{
strcpy(message,"Error on binding");
eFlag = 1;
send_message(message);
}
listen(sockfd, 5);
clilen = sizeof(client_addr);
newsockfd = accept(sockfd, (struct sockaddr *)&client_addr, &clilen);
if(newsockfd < 0)
{
strcpy(message,"Error on accept");
eFlag = 1;
send_message();
exit(4);
}
void send_file()
{
buffer = (char *)malloc(sizeof(char)*lSize);
result = fread(buffer, sizeof(char), lSize, fp);
printf("\n\n########## data read into buffer successfully #######");
if(newsockfd)
{
n = send(newsockfd, buffer, lSize);
printf("\n\n$$$$$$$$ Data sent successfully $$$$$");
strcpy(message,"Data sent successfully");
send_message(message);
}
else
{
strcpy(message, "Error writing on socket");
send_message(message);
}
sleep(sleepTime);
sleepTime = (int) (sleepTime*1.02);
free(buffer);
}
And, my client side Java code is:
final int PORT_NO = 6789;
final String Server_name = "192.133.133.1";
Socket m_socket = null;
String str;
int ch;
try {
if(m_socket == null)
m_socket = new Socket(Server_name, PORT_NO);
if(m_socket == null)
System.out.println("Unable to open the socket");
DataInputStream dis = new DataInputStream(m_socket.getInputStream());
PrintStream ps = new PrintStream(m_socket.getOutputStream());
DataInputStream ds = new DataInputStream(System.in);
while(true)
{
System.out.println("1. Synchronize");
System.out.println("2. Exit");
System.out.println("Enter your choice...");
str = ds.readLine();
ch = Integer.parseInt(str);
switch(ch)
{
case 1:
ps.println("<message action='buttonpress' value='synchronize' />");
System.out.println("avilable data to read is:"+dis.available());
FileOutputStream fos = new FileOutputStream("mukul.db");
byte data[] = new byte[102400]; //100 Kb byte array
dis.read(data);
String str_data = new String(data);
str_data = str_data.trim();
data = str_data.getBytes();
fos.write(data);
fos.close();
break;
here only part of data is read i.e around 10 kb or less.
I am just a beginner to such posts and my code may be cumbersome, so please ignore all the mistakes in posting.
So please kindly tell me how can i receive 1 MB/10 MB of data in this client-server architecture without the loss of data.
What if i use "sendfile(out_fp, in_fp, pos, len)" method in C code instead of "send()". This method sends file handle. So what will be the corresponding function in Java to capture file handle.
Thank you in advance.