2015-04-28 51 views
0

如何使用tcp/ip在两个不同系统之间建立客户端 - 服务器通信?我已经完成了服务器和客户端的代码,但是当我运行代码时,没有错误,而且我也无法看到该文件。我附上代码以供进一步参考。如何使用tcp/ip在两台不同的计算机之间运行客户端服务器通信?

Server代码:

#include<stdio.h> 
#include<string.h> 
#include<sys/types.h> 
#include<sys/socket.h> 
#include<arpa/inet.h> 
#include<stdlib.h> 
#include<sys/ioctl.h> 
#include<stdarg.h> 
#include<setjmp.h> 
#include<errno.h> 
#include<assert.h> 

//using namespace std; 

long long int send_audio(long long int socket) 

{ 

    FILE *audio; 
    long long int size, read_size, stat, packet_index; 
    char send_buffer[1024000], read_buffer[1024000]; 
    packet_index = 1; 

    audio = fopen("/home/sosdt009/Desktop/ROBO.mp3", "r"); 

    printf("Getting audio Size\n"); 

    if(audio == NULL) 

    { 

     printf("Error Opening audio File"); 
    } 

    fseek(audio, 0, SEEK_END); 
    size = ftell(audio); 
    fseek(audio, 0, SEEK_SET); 
    printf("Total audio size: %lli \n", size); 

    //Send audio Size 

    printf("Sending audio Size\n"); 
    write(socket, (void *)&size, sizeof(int)); 

    //Send audio as Byte Array 

    printf("Sending audio as Byte Array\n"); 

    do 
    { 

     //Read while we get errors that are due to signals. 

     stat = read(socket, &read_buffer, 255); 
     printf("Bytes read: %lli \n", stat); 

    } 

    while(stat < 0); 

    printf("Received data in socket\n"); 
    printf("Socket data: %s\n", read_buffer); 

    while(!feof(audio)) 

    { 

     //while(packet_index = 1){ 
     //Read from the file into our send buffer 

     read_size = fread(send_buffer, 1, sizeof(send_buffer) - 1, audio); 

     //Send data through our socket 

     do 

     { 

      stat = write(socket, send_buffer, read_size); 

     } 

     while(stat < 0); 

     printf("Packet Number: %lli \n", packet_index); 
     printf("Packet Size Sent: %lli \n", read_size); 
     printf(" \n"); 
     printf(" \n"); 


     packet_index++; 

     //Zero out our send buffer 

     bzero(send_buffer, sizeof(send_buffer)); 

    } 

} 

long long int main(int argc, char *argv[]) 

{ 
    long long int socket_desc, new_socket, c, read_size, buffer = 0; 
    struct sockaddr_in server, client; 
    char *readin; 

    //Create socket 

    socket_desc = socket(AF_INET, SOCK_STREAM, 0); 
    if(socket_desc == -1) 

    { 

     printf("Could not create socket"); 

    } 

    //Prepare the sockaddr_in structure 

    server.sin_family = AF_INET; 
    server.sin_addr.s_addr = INADDR_ANY; 
    server.sin_port = htons(6888); 

    //Bind 

    if(bind(socket_desc, (struct sockaddr *)&server, sizeof(server)) < 0) 

    { 

     puts("bind failed"); 
     return 1; 

    } 

    puts("Bind completed"); 

    //Listen 

    listen(socket_desc, 3); 

    //Accept and incoming connection 

    puts("Waiting for incoming connections..."); 
    c = sizeof(struct sockaddr_in); 

    if((new_socket = accept(socket_desc, (struct sockaddr *)&client, (socklen_t *)&c))) 

    { 

     puts("Connection accepted"); 

    } 

    fflush(stdout); 

    if(new_socket < 0) 

    { 

     perror("Accept Failed"); 
     return 1; 

    } 

    send_audio(new_socket); 

    close(socket_desc); 
    fflush(stdout); 
    return 0; 
}  

客户端代码:

#include<stdio.h> 
#include<string.h>  
#include<stdlib.h> 
#include<sys/socket.h> 
#include<sys/ioctl.h> 
#include<sys/types.h> 
#include<arpa/inet.h>  
#include<unistd.h> 

//using namespace std; 

//This function is to be used once we have confirmed that an audio is to be sent //It should read and output an audio file 

long long int receive_audio(long long int socket) 

{ 

    long long int buffersize = 0, recv_size = 0, size = 0, read_size, write_size; 

    char audioarray[1024000], verify = '1', errno; 

    FILE *audio; 

    //Find the size of the audio 

    read(socket, &size, sizeof(int)); 

    //Send our verification signal 

    write(socket, &verify, sizeof(char)); 

    //Make sure that the size is bigger than 0 

    if(size <= 0) 

    { 

     printf("Error has occurred. Size less than or equal to 0 \n");  return -1; 

    } 

    audio = fopen("/home/sosdt009/Desktop/received.mp3", "w"); 

    if(audio == NULL) 

    { 

     printf("Error has occurred. audio file could not be opened \n");  return -1; 

    } 

    //Loop while we have not received the entire file yet 

    while(recv_size < size) 

    { 

     ioctl(socket, FIONREAD, &buffersize); 

     //We check to see if there is data to be read from the socket  

     if(buffersize > 0) 

     { 

      if(read_size = read(socket, audioarray, buffersize) < 0){ 
       printf("%s", strerror(errno)); 

      } 

      //Write the currently read data into our audio file 

      write_size = fwrite(audioarray, 1, (buffersize), audio); 

      /* if(write_size != buffersize) 

      { 

      printf("write and buffer sizes are wrong \n"); 

      } 

      if(read_size != write_size) 

      { 

      printf("error in read write \n"); 

      } */ 

      //Increment the total number of bytes read 

      recv_size += read_size; 

      //Send our handshake verification info 

      write(socket, &verify, sizeof(char)); 

     } } 

    fclose(audio); printf("audio successfully Received! \n"); return 1; } 

long long int main(int argc, char *argv[]) { 

    long long int socket_desc; struct sockaddr_in server; char *parray, errno; 


    //Create socket 

    socket_desc = socket(AF_INET, SOCK_STREAM, 0); 

    if(socket_desc == -1) 

    { 

     printf("Could not create socket \n"); 

    } 

    memset(&server, 0, sizeof(server));  server.sin_addr.s_addr = ("10.170.0.40");; server.sin_family = AF_INET; server.sin_port = htons(6888); 

    //Connect to remote server 

    if(connect(socket_desc, (struct sockaddr *)&server, sizeof(server)) < 0) 

    { 


     // printf(strerror(errno)); 

     close(socket_desc); 

     printf("Connect Error \n"); 

     return -1; } 

    puts("Connected \n"); 

    receive_audio(socket_desc); 

    close(socket_desc); 

    return 0; } 
+0

你打印出大量的信息。那么你看到了什么?怎么了 ? – nos

+1

请修复您的格式问题,iit很难读取代码 – Bruce

+1

'long long int main' ??? – Levi

回答

0

while (stat < 0);while (stat > 0); - 乔普

相关问题