java客户端和c服务器之间的文件传输

时间:2014-12-16 06:33:29

标签: java c sockets client-server file-transfer

我正在尝试将文件从java客户端发送到c服务器......问题是我的c服务器一次只接收1个字节而不管缓冲区大小......我想转移块中的数据(可能是512字节或更多)......有人可以看看并指出什么是错的或必须改变的内容......提前感谢...

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 to child thread %i with 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 from child thread %i with pid %i.\n", pthread_self(), getpid());
        return NULL;
    }

java客户端:

    import java.net.*;
    import java.io.*;
    import java.util.Arrays;

    // A client for our multithreaded EchoServer.
    public class client
    {
        public static void main(String[] args)
        {
            Socket socket = null;
            int PORT = 8880;

           // Create the socket connection to the EchoServer.
            try
            {
                socket = new Socket("localhost", PORT);
            }        
            catch(UnknownHostException uhe)
            {
                // Host unreachable
                System.out.println("Unknown Host");
                socket = null;
            }
            catch(IOException ioe)
            {
                // Cannot connect to port on given host
                System.out.println("Cant connect to server at 8880. Make sure it is 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 probably closed connection.");
            }
            finally
            {
                try
                {
                    // Close the socket before quitting
                    socket.close();
                }
                catch(Exception e)
                {
                    e.printStackTrace();
                }                
            }        
        }
    }

1 个答案:

答案 0 :(得分:0)

检查这些代码

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 to child thread %i with 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 from child thread %i with pid %i.\n", pthread_self(), getpid());
    return NULL;
}

客户端

 import java.net.*;
    import java.io.*;
    import java.util.Arrays;

    // A client for our multithreaded EchoServer.
    public class client
    {
        public static void main(String[] args)
        {
            Socket socket = null;
            int PORT = 5000;

           // Create the socket connection to the EchoServer.
            try
            {
                socket = new Socket("localhost", PORT);
            }        
            catch(UnknownHostException uhe)
            {
                // Host unreachable
                System.out.println("Unknown Host");
                socket = null;
            }
            catch(IOException ioe)
            {
                // Cannot connect to port on given host
                System.out.println("Cant connect to server at 5000. Make sure it is 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 probably closed connection.");
            }
            finally
            {
                try
                {
                    // Close the socket before quitting
                    socket.close();
                }
                catch(Exception e)
                {
                    e.printStackTrace();
                }                
            }        
        }
    }

我刚使用端口5000并使用nc命令检查你的服务器..它工作正常

我认为你需要做的就是改变你的PORT。我检查了运行它们:)

- 快乐编码 -