problem in image,pdf,.iso file transfer from java client to c++ server (2 machine)
i am trying to send all kind of files from java client to c++ server. all kind of text files are transferred successfully. but when it comes to pdf,image,.iso files, it produces errors like pdf load error, cannot open image, not in .iso format . below i am attaching the c++ server code and java client code.please help me to rectify it.
client1.java
import java.net.*;
import java.io.*;
class client1{
public static void main (String[] args){
DataInputStream input;
String str;
BufferedInputStream bis;
BufferedOutputStream bos;
int in;
byte[] byteArray;
try{
Socket client = new Socket(args[0],Integer.parseInt(args[1]));
System.out.println("enter file");
DataInputStream din1=new DataInputStream(System.in);
str=din1.readLine();
bis = new BufferedInputStream(new FileInputStream(str));
bos = new BufferedOutputStream(client.getOutputStream());
byteArray = new byte[1024];
while ((in = bis.read(byteArray)) != -1){
bos.write(byteArray,0,in);
Thread.sleep(4000);
System.out.println(byteArray);
bos.flush();
}
System.out.println("send successfully");
}
catch ( Exception e ) { }
}}
server1.cpp
#include <iostream>
#include <fstream>
#include <string.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<stdlib.h>
#include<arpa/inet.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<stdio.h>
using namespace std;
int main()
{
int create_socket,new_socket,fd,m,l;
socklen_t addrlen;char msg[1024];
struct sockaddr_in address;
if ((create_socket = socket(AF_INET,SOCK_STREAM,0)) > 0)
cout<<"The socket was created\n";
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;//INADDR_ANY;inet_addr("localhost");
address.sin_port = htons(1238);
cout<<"\nworking";
if (bind(create_socket,(struct sockaddr *)&address,sizeof(address)) == 0)
cout<<"Binding Socket\n";
listen(create_socket,3);
addrlen = sizeof(struct sockaddr_in);
cout<<"*************************\n";
new_socket = accept(create_socket,(struct sockaddr *)&address,&addrlen);
cout<<"*************************\n";
if (new_socket > 0)
{
char buf[1024];int ch;
cout<<"The Client "<<inet_ntoa(address.sin_addr)<<" is Connected...\n";//inet_ntoa(address.sin_addr));
FILE *fp;
fp=fopen("ren.txt","w");
char message[1024],message1[1024];
ssize_t bytes,byte;
bytes=1;
{
while(bytes!=0)
{
bytes=recvfrom(new_socket,&message, 1024, 0, (struct sockaddr *)&address, &addrlen);
cout<<"hai";
cout<<message;
printf("msg from %s:%d (%d bytes)\n", inet_ntoa(address.sin_addr),ntohs(address.sin_port), bytes);
//outputFile.write(message,sizeof(message));
fwrite(message,1,strlen(message),fp );
memset(message,0x0,sizeof(message));
}
outputFile.close();
fclose(fp);
}
}
close(new_socket);
return close(create_socket);
}
|