File transfer between java client and c server -


i trying send file java client c server......the issue c server receiving 1 byte @ time irrespective of buffer size......i wanna tranfer data in chunks (may 512 bytes or more) @ time......can take , point out whats wrong or must changed....thanks in advance...

c server:

    #include <stdio.h>     #include <string.h>     #include <stdlib.h>     #include <sys/ioctl.h>     #include <sys/types.h>     #include <sys/socket.h>     #include <netinet/in.h>     #include <pthread.h>      #define port 8880     #define filename "incoming.txt"      void* thread_proc(void *arg);      int main(int argc, char *argv[])     {         struct sockaddr_in saddr;         int sockfd,connfd;         int status;         pthread_t thread_id;         int val;          sockfd = socket(af_inet, sock_stream, ipproto_tcp);          val = 1;         status = setsockopt(sockfd, sol_socket, so_reuseaddr, &val, sizeof(val));         if (status < 0) {             perror("error - port");             return 0;         }          saddr.sin_family = af_inet;         saddr.sin_port = htons(port);         saddr.sin_addr.s_addr = inaddr_any;          status = bind(sockfd, (struct sockaddr *) &saddr, sizeof(saddr));         if (status < 0) {             perror("error - bind");             return 0;         }          status = listen(sockfd, 5);         if (status < 0) {             perror("error - listen");             return 0;         }         while(1) {            connfd = accept(sockfd, null, null);            if (connfd < 0) {                 printf("accept error on server\n");                error("error on accept");                 return 0;            }            printf("client connected child thread %i pid %i.\n", pthread_self(), getpid());            status = pthread_create(&thread_id, null, thread_proc, (void *) connfd);            if (status != 0) {                printf("could not create thread.\n");                return 0;            }            sched_yield();         }         pthread_join (thread_id, null);     }      void* thread_proc(void *arg)     {         int connfd;         int nread,n;         char buffer[1024];         file *fp;          connfd = (int) arg;          fp = fopen(filename, "ab");          if (fp == null)          {             printf("file not found!\n");             return null;         }         else          {             printf("found file %s\n", filename);         }          while (n = recv(connfd, buffer, sizeof buffer, 0) > 0) {                 fwrite(buffer, sizeof(char), n, fp);                 fprintf(stdout, "received %d bytess\n", n);         }         fclose(fp);         close(connfd);         printf("client disconnected child thread %i pid %i.\n", pthread_self(), getpid());         return null;     } 

java client:

    import java.net.*;     import java.io.*;     import java.util.arrays;      // client our multithreaded echoserver.     public class client     {         public static void main(string[] args)         {             socket socket = null;             int port = 8880;             // create socket connection echoserver.             try             {                 socket = new socket("localhost", port);             }                     catch(unknownhostexception uhe)             {                 // host unreachable                 system.out.println("unknown host");                 socket = null;             }             catch(ioexception ioe)             {                 // cannot connect port on given host                 system.out.println("cant connect server @ 8880. make sure running.");                 socket = null;             }              if(socket == null)                 system.exit(-1);             try             {                 file file = new file("ext.txt");                 byte[] bytes = new byte[8192];                 fileinputstream fis = new fileinputstream(file);                 bufferedinputstream bis = new bufferedinputstream(fis);                 outputstream out = socket.getoutputstream();                  int count,file_size;                  while ((count = bis.read(bytes)) > 0) {                     system.out.println(count);                     out.write(bytes, 0, count);                 }                 out.flush();                 out.close();                 fis.close();                 bis.close();             }               catch(ioexception ioe)             {                 system.out.println("exception during communication. server closed connection.");             }                         {                 try                 {                     // close socket before quitting                     socket.close();                 }                 catch(exception e)                 {                     e.printstacktrace();                 }                             }                 }     } 

check these codes

server

#include <stdio.h> #include <string.h> #include <stdlib.h> #include <sys/ioctl.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <pthread.h> #include <error.h>  #define port 5000 #define filename "incoming.txt"  void* thread_proc(void *arg);  int main(int argc, char *argv[]) {     struct sockaddr_in saddr;     int sockfd,connfd;     int status;     pthread_t thread_id;     int val;      sockfd = socket(af_inet, sock_stream, ipproto_tcp);      val = 1;     status = setsockopt(sockfd, sol_socket, so_reuseaddr, &val, sizeof(val));     if (status < 0) {         perror("error - port");         return 0;     }      saddr.sin_family = af_inet;     saddr.sin_port = htons(port);     saddr.sin_addr.s_addr = inaddr_any;      status = bind(sockfd, (struct sockaddr *) &saddr, sizeof(saddr));     if (status < 0) {         perror("error - bind");         return 0;     }      status = listen(sockfd, 5);     if (status < 0) {         perror("error - listen");         return 0;     }     while(1) {        connfd = accept(sockfd, null, null);        if (connfd < 0) {             printf("accept error on server\n");            //error("error on accept");             return 0;        }        printf("client connected child thread %i pid %i.\n", pthread_self(), getpid());        status = pthread_create(&thread_id, null, thread_proc, (void *) connfd);        if (status != 0) {            printf("could not create thread.\n");            return 0;        }        sched_yield();     }     pthread_join (thread_id, null); }  void* thread_proc(void *arg) {     int connfd;     int nread,n;     char buffer[1000];     file *fp;      connfd = (int) arg;      fp = fopen(filename, "ab");      if (fp == null)      {         printf("file not found!\n");         return null;     }     else      {         printf("found file %s\n", filename);     }      while ((n = recv(connfd, buffer, sizeof buffer, 0)) > 0) {             fwrite(buffer, sizeof(char), n, fp);             fprintf(stdout, "received %d bytess\n", n);     }     fclose(fp);     close(connfd);     printf("client disconnected child thread %i pid %i.\n", pthread_self(), getpid());     return null; } 

client

 import java.net.*;     import java.io.*;     import java.util.arrays;      // client our multithreaded echoserver.     public class client     {         public static void main(string[] args)         {             socket socket = null;             int port = 5000;             // create socket connection echoserver.             try             {                 socket = new socket("localhost", port);             }                     catch(unknownhostexception uhe)             {                 // host unreachable                 system.out.println("unknown host");                 socket = null;             }             catch(ioexception ioe)             {                 // cannot connect port on given host                 system.out.println("cant connect server @ 5000. make sure running.");                 socket = null;             }              if(socket == null)                 system.exit(-1);             try             {                 file file = new file("ext.txt");                 byte[] bytes = new byte[8192];                 fileinputstream fis = new fileinputstream(file);                 bufferedinputstream bis = new bufferedinputstream(fis);                 outputstream out = socket.getoutputstream();                  int count,file_size;                  while ((count = bis.read(bytes)) > 0) {                     system.out.println(count);                     out.write(bytes, 0, count);                 }                 out.flush();                 out.close();                 fis.close();                 bis.close();             }               catch(ioexception ioe)             {                 system.out.println("exception during communication. server closed connection.");             }                         {                 try                 {                     // close socket before quitting                     socket.close();                 }                 catch(exception e)                 {                     e.printstacktrace();                 }                             }                 }     } 

i used port 5000 , used nc command check server.. works fine

i think need changing port. checked running them :)

-happy coding-


Comments

Popular posts from this blog

java - Plugin org.apache.maven.plugins:maven-install-plugin:2.4 or one of its dependencies could not be resolved -

Round ImageView Android -

How can I utilize Yahoo Weather API in android -