使用c套接字编程发送char数组时出错

时间:2015-04-23 13:47:48

标签: c

下面是一个用于将char数组从客户端传输到服务器的代码,但是服务器无法接收数组并显示接收到的数组的内容,还有如下警告:

assignment makes integer from pointer without a cast [enabled by default]
     temp_char_arr[i]=recvs_bytes[i];

请帮我解决这个问题

提前感谢。

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include<signal.h>

#define PORT 2021

int sd,ad,pid,len = 0,i;
float p;
char buf[100];
char* recvs_bytes[11],temp_char_arr[11];


void save_bytes(const char *filename,char* temp_char_arr[]);

void Die(char *mess)
{
    perror(mess); exit(1); 
}
int main()
{

    printf("\nEnter server port:2020 and server          ip:192.168.204.138\n");        
    struct sockaddr_in serverAddress;
    if((sd = socket(AF_INET,SOCK_STREAM,0)) < 0)
    {
        Die("Error creating socket");
    }
    memset(&serverAddress, 0, sizeof(serverAddress));
    serverAddress.sin_family = AF_INET;
    serverAddress.sin_port = htons(PORT);
    serverAddress.sin_addr.s_addr = htonl(INADDR_ANY);

    if(bind(sd, (struct sockaddr*)&serverAddress, sizeof(serverAddress)) < 0)
    {
        Die("Bind error");
    }

    if(listen(sd, 5) < 0)
    {
        Die("Listen error");
    }
    printf("\n***************************serveronline*****************************\n");
    while(1)
    {
        ad = accept(sd, (struct sockaddr*)&serverAddress, &len);
        pid=fork();
        if(pid<0) 
            Die("Fork error");

        if(pid==0)
        {
            p=1.0;
            if(send(ad,&p,sizeof(float),0)< 0)  Die("Error sending data");
            printf("\nreceivieng bytes\n");
            if(recv(ad, recvs_bytes, sizeof(recvs_bytes), 0) <0) Die("Receive error");
            for(i=0;i<=10;i++)
            {
                temp_char_arr[i]=recvs_bytes[i];
                printf("%s",&temp_char_arr[i]);
            }
            printf("\nbytes received\n");
            close(sd);
            exit(0);
        }
        if(pid>0)
        {
            close(ad);
        }
    }

    return 0;
}

client.c

#include <stdio.h>
#include <sys/statfs.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <signal.h>
#include <netinet/in.h>
#include <string.h>
#include <stdlib.h>
#include<signal.h>


char *trans_bytes[11],arr[11];
FILE *ptr_file;
char* temp[11],arr[11];
int sd,i;
float q;

void func();
void send_trans_bytes(char* arr[]);

void Die(char *mess) { perror(mess); exit(1); }

int main(int argc, char *argv[])
{
    if (argc != 3) {
        printf("USAGE: TCPecho <server_ip> <port>\n");
        exit(1);
    }

    struct sockaddr_in clientAddress;
    if((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
    {
        Die("Error creating socketsss");
    }

    memset((char *)&clientAddress, 0, sizeof(clientAddress));

    clientAddress.sin_family = AF_INET;
    clientAddress.sin_addr.s_addr= inet_addr(argv[1]);
    clientAddress.sin_port = htons(atoi(argv[2]));
    if ((connect(sd,(struct sockaddr *)&clientAddress, sizeof(clientAddress))) < 0)
    {
        Die("Error connectiong to server!");
    }
    printf("\n--------------------client online----------------------------\n");

    if(recv(sd, &q, sizeof(float), 0) < 0)  {    Die("Receive error");   }
    if (q==1.0)
        printf("\nsignal receivied=%f\n",q);
    func();
    return 0;
}


void func()
{
    int i;
    for(i=0;i<=10;i++)
        temp[i]="some_text";
    send_trans_bytes(temp);
}


void send_trans_bytes(char* arr[])
{
    if(send(sd,arr, sizeof(arr), 0) < 0) Die("Error sending host name");
    for(i=0;i<=10;i++)
        printf("arr[%d]=%s",i,arr[i]);
}

0 个答案:

没有答案