在c中使用UDP和TCP套接字编程建立连接

时间:2017-04-28 02:27:58

标签: c sockets

我试图在相同的代码中建立UDP和TCP连接。我有3个来自server.ctransfer.cclient.c的来源的程序。我希望首先server运行并等待数据;然后transfer然后client

我想要的结果是,如果我在client中键入消息,它会使用TCP发送到transfertransfer使用TCP将此消息转发给服务器。当server收到邮件时,它会检查邮件是否为字母或数字。如果它是alpha,server发送"这不是数字"使用UDP向client发送消息。如果它是一个数字,它只是使用UDP将相同的消息转发到client

我能够使用TCP向server发送消息,然后我无法使用UDP向client发送特定消息。知道什么是错的吗?

这是我的代码。这是我的server.c

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

#define PORTNUM 2400
#define MAXRCVLEN 500
#define SERVER "127.0.0.1" // local server
#define BUFLEN 512  //Max length of buffer
#define PORT 2500   //The port to establish UDP conection

void DieWithError(char *s)
{
perror(s);
exit(1);
}


int main(int argc, char *argv[])
{
char* msg = "HI \n"; 
char buffer[MAXRCVLEN + 1];

int len, consocket;
struct sockaddr_in dest;
struct sockaddr_in serv;
int mysocket;
socklen_t socksize = sizeof(struct sockaddr_in);

memset(&serv, 0, sizeof(serv));
serv.sin_family = AF_INET;
serv.sin_addr.s_addr = htonl(INADDR_ANY);
serv.sin_port = htons(PORTNUM);

mysocket = socket(AF_INET, SOCK_STREAM, 0);

bind(mysocket, (struct sockaddr *)&serv, sizeof(struct sockaddr));
///////////////////////////////////////////////////////////
//UDP initialiazation to forward received message to client
struct sockaddr_in si_other;
int s, i, slen=sizeof(si_other), recv_len;
char buf[BUFLEN];
char message[BUFLEN];
int num; //number of messages   

s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);

//creates socket
memset((char *) &si_other, 0, sizeof(si_other));
si_other.sin_family = AF_INET;
si_other.sin_port = htons(PORT);

inet_aton(SERVER , &si_other.sin_addr);


    /*{
        DieWithError("sendto() failed");
    }*/


 ///////////////////////////////////////////////////////////

do{
listen(mysocket, 1);
consocket = accept(mysocket, (struct sockaddr *)&dest, &socksize);
send(consocket, msg, strlen(msg), 0);
    while(len=recv(consocket, buffer, MAXRCVLEN, 0)>0){
            if(strncmp(buffer, "exit", 4) == 0){ 
        break;
    }

           printf("Received: %s ", buffer);
         //checks the received message alpha or digit
            if(isalpha(buf[0])){
        strcpy(buf, "this is not a digit");//sends this message to client 
        //if the message is alphs
        }



           //udp sends function to send the msg using UDP
            if (sendto(s, message, strlen(message) , 0 , (struct sockaddr
   *) &si_other, slen)==-1)
    {
        DieWithError("sendto() failed");
    }
    //clear the buffer
    memset(buf,'\0', BUFLEN);
  //memset(buffer, 0, strlen(buffer));


    }
  printf("Goodbye !\n");
    close(consocket);

}while(consocket);

close(mysocket);
return EXIT_SUCCESS;
}

这是我的transfer.c

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

#define PORTNUM1 2300
#define PORTNUM2 2400
#define MAXRCVLEN 500


int main(int argc, char *argv[])
{
char* msg = "HI from Inter-server\n"; 
char buffer[MAXRCVLEN + 1];

int len, consocket;
struct sockaddr_in dest;
struct sockaddr_in serv;

struct sockaddr_in client;

int server_socket;
int client_socket;

// For server connection
socklen_t socksize = sizeof(struct sockaddr_in);

memset(&serv, 0, sizeof(serv));
serv.sin_family = AF_INET;
serv.sin_addr.s_addr = htonl(INADDR_ANY);
serv.sin_port = htons(PORTNUM1);
server_socket = socket(AF_INET, SOCK_STREAM, 0);
bind(server_socket, (struct sockaddr *)&serv, sizeof(struct sockaddr));
////////////////////////////////////////////////////////////
//  code for for client connection 
// socket, connect, and recv here
client_socket = socket(AF_INET, SOCK_STREAM, 0);

 memset(&client, 0, sizeof(client));
 client.sin_family = AF_INET;
 client.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
 client.sin_port = htons(PORTNUM2);

 connect(client_socket, (struct sockaddr *)&client, sizeof(struct 
 sockaddr));

recv(client_socket, msg, MAXRCVLEN, 0);

///////////////////////////////////////////////////////////


// printf("As a client, I Received %s \n", buffer);
// memset(buffer, 0, strlen(buffer));

printf("Server starts...\n");

do{
listen(server_socket, 1);
consocket = accept(server_socket, (struct sockaddr *)&dest, &socksize);
send(consocket, msg, strlen(msg), 0);
    while(len=recv(consocket, buffer, MAXRCVLEN, 0)>0){
            printf("Received: %s ", buffer);

// As a client, send "buffer" here

           send(client_socket, buffer, strlen(buffer), 0);


           if(strncmp(buffer, "exit", 4) == 0) break;


      memset(buffer, 0, strlen(buffer));

       }
printf("Goodbye !\n");
    close(consocket);

}while(consocket);

close(client_socket);
close(server_socket);
return EXIT_SUCCESS;
}

这是我的client.c

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

#define MAXRCVLEN 500
#define PORTNUM 2300
#define BUFLEN 512  //Max length of buffer
#define PORT 2500   //The port  to establish UDP connection

void DieWithError(char *s)
{
    perror(s);
exit(1);
}


int main(int argc, char *argv[])
{
char buffer[MAXRCVLEN + 1]; // for receiving
int len, mysocket;
struct sockaddr_in dest;
char msg[MAXRCVLEN + 1]; // for sending

mysocket = socket(AF_INET, SOCK_STREAM, 0);

memset(&dest, 0, sizeof(dest));
dest.sin_family = AF_INET;
dest.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
dest.sin_port = htons(PORTNUM);

connect(mysocket, (struct sockaddr *)&dest, sizeof(struct sockaddr));
/////////////////////////////////////////////////////////
//UDP initialization 
  struct sockaddr_in si_me, si_other;

  int s, i, slen = sizeof(si_other) , recv_len;
  char message[BUFLEN];
  //int num=0;
  char buf[BUFLEN];
 //create a UDP socket
 if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
{
    DieWithError("socket() failed");
}

// zero out the structure
memset((char *) &si_me, 0, sizeof(si_me));

si_me.sin_family = AF_INET;
si_me.sin_port = htons(PORT);
si_me.sin_addr.s_addr = htonl(INADDR_ANY);

//bind socket to port
if( bind(s , (struct sockaddr*)&si_me, sizeof(si_me) ) == -1)
{
    DieWithError("bind() failed");
}



//check digits or alpha



/////////////////////////////////////////////////////////
len = recv(mysocket, buffer, MAXRCVLEN, 0);
if(len < 0){
    perror("Reciver failed");
    close(mysocket);
    return EXIT_FAILURE;
}
else{
    buffer[len] = '\0';
    printf("I Received %s \n", buffer);
 }


            //printf("Received: %s ", buffer);


 while(1){
    printf("Enter a character ");
    fgets(msg, sizeof(msg), stdin);
    if(strncmp(msg, "exit", 4) == 0) break;
    if(send(mysocket, msg, strlen(msg), 0) < 0){
            perror("Send failed");
            close(mysocket);
            return EXIT_FAILURE;
    }

 memset(msg, 0, strlen(msg));

}
if ((recv_len = recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *) &si_other,
&slen)) == -1 )//to receive message from server as UDP
    {
        DieWithError("recvfrom() failed");
    }
  printf("Received a packet from %s:%d\n", inet_ntoa(si_other.sin_addr),
  ntohs(si_other.sin_port));
    printf("Data: %s\n" , buf);

 //printf("Received: %s ", buffer);    
 close(mysocket);
return EXIT_SUCCESS;
}

1 个答案:

答案 0 :(得分:-1)

我不认为你的编码风格很好。以下可能有效。

server.c

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

#define PORTNUM 2400
#define MAXRCVLEN 500
#define SERVER "127.0.0.1" // local server
#define BUFLEN 512  //Max length of buffer
#define PORT 2500   //The port to establish UDP conection

void DieWithError(char *s)
{
    perror(s);
    exit(1);
}


int main(int argc, char *argv[])
{
    char* msg = "HI \n"; 
    char buffer[MAXRCVLEN + 1];

    int len, consocket;
    struct sockaddr_in dest;
    struct sockaddr_in serv;
    int mysocket;
    socklen_t socksize = sizeof(struct sockaddr_in);

    memset(&serv, 0, sizeof(serv));
    serv.sin_family = AF_INET;
    serv.sin_addr.s_addr = htonl(INADDR_ANY);
    serv.sin_port = htons(PORTNUM);

    mysocket = socket(AF_INET, SOCK_STREAM, 0);

    bind(mysocket, (struct sockaddr *)&serv, sizeof(struct sockaddr));
///////////////////////////////////////////////////////////
//UDP initialiazation to forward received message to client
    struct sockaddr_in si_other;
    int s, i, slen=sizeof(si_other), recv_len;
    char buf[BUFLEN];
    char message[BUFLEN];
    int num; //number of messages   

    s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);

//creates socket
    memset((char *) &si_other, 0, sizeof(si_other));
    si_other.sin_family = AF_INET;
    si_other.sin_port = htons(PORT);

    inet_aton(SERVER , &si_other.sin_addr);


    /*{
        DieWithError("sendto() failed");
    }*/


 ///////////////////////////////////////////////////////////

    do{
        listen(mysocket, 1);
        consocket = accept(mysocket, (struct sockaddr *)&dest, &socksize);
        send(consocket, msg, strlen(msg), 0);
            while((len=recv(consocket, buffer, MAXRCVLEN, 0))>0){
                if( strncmp(buffer, "exit", 4) == 0) 
                    break;

            buffer[len] = '\0';
                printf("Received: %s", buffer);
         //checks the received message alpha or digit
                memset(buf,0, BUFLEN);
                    if(isalpha(buffer[0])){
                    strcpy(buf, "this is not a digit");//sends this message to client 
        //if the message is alphs
                }
            else {
                    strcpy(buf, "this is a digit");//sends this message to client 
            }

            //puts(buf);

           //udp sends function to send the msg using UDP
                    if (sendto(s, buf, strlen(buf) , 0 , (struct sockaddr*) &si_other, slen)==-1)
                {
                    DieWithError("sendto() failed");
                }
    //clear the buffer
  //memset(buffer, 0, strlen(buffer));
        }

            close(consocket);

    }while(consocket);

    printf("Goodbye !\n");
    close(mysocket);
    return EXIT_SUCCESS;
}

Transfer.c

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

#define PORTNUM1 2300
#define PORTNUM2 2400
#define MAXRCVLEN 500


int main(int argc, char *argv[])
{
    char* msg = "HI from Inter-server\n"; 
    char buffer[MAXRCVLEN + 1];

    int len, consocket;
    struct sockaddr_in dest;
    struct sockaddr_in serv;

    struct sockaddr_in client;

    int server_socket;
    int client_socket;

    // For server connection
    socklen_t socksize = sizeof(struct sockaddr_in);

    memset(&serv, 0, sizeof(serv));
    serv.sin_family = AF_INET;
    serv.sin_addr.s_addr = htonl(INADDR_ANY);
    serv.sin_port = htons(PORTNUM1);
    server_socket = socket(AF_INET, SOCK_STREAM, 0);
    bind(server_socket, (struct sockaddr *)&serv, sizeof(struct sockaddr));
    ////////////////////////////////////////////////////////////
    //  code for for client connection 
    // socket, connect, and recv here
    client_socket = socket(AF_INET, SOCK_STREAM, 0);

    memset(&client, 0, sizeof(client));
    client.sin_family = AF_INET;
    client.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
    client.sin_port = htons(PORTNUM2);

    connect(client_socket, (struct sockaddr *)&client, sizeof(struct sockaddr));

    recv(client_socket, msg, MAXRCVLEN, 0);

    ///////////////////////////////////////////////////////////


    // printf("As a client, I Received %s \n", buffer);
    // memset(buffer, 0, strlen(buffer));

    printf("Server starts...\n");

    do{
        listen(server_socket, 1);
        consocket = accept(server_socket, (struct sockaddr *)&dest, &socksize);
        send(consocket, msg, strlen(msg), 0);
        while((len=recv(consocket, buffer, MAXRCVLEN, 0))>0){
            printf("Received: %s ", buffer);
    // As a client, send "buffer" here
            send(client_socket, buffer, strlen(buffer), 0);
            if(strncmp(buffer, "exit", 4) == 0) 
                break;
            memset(buffer, 0, strlen(buffer));
        }
        printf("Goodbye !\n");
            close(consocket);
    }while(consocket);

    close(client_socket);
    close(server_socket);

    return EXIT_SUCCESS;
}

client.c

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

#define MAXRCVLEN 500
#define PORTNUM 2300
#define BUFLEN 512  //Max length of buffer
#define PORT 2500   //The port  to establish UDP connection

void DieWithError(char *s)
{
        perror(s);
    exit(1);
}


int main(int argc, char *argv[])
{
    char buffer[MAXRCVLEN + 1]; // for receiving
    int len, mysocket;
    struct sockaddr_in dest;
    char msg[MAXRCVLEN + 1]; // for sending

    mysocket = socket(AF_INET, SOCK_STREAM, 0);

    memset(&dest, 0, sizeof(dest));
    dest.sin_family = AF_INET;
    dest.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
    dest.sin_port = htons(PORTNUM);

    connect(mysocket, (struct sockaddr *)&dest, sizeof(struct sockaddr));
/////////////////////////////////////////////////////////
//UDP initialization 
    struct sockaddr_in si_me, si_other;

    int s, i, recv_len;
    socklen_t slen = sizeof(si_other);
    char message[BUFLEN];
  //int num=0;
    char buf[BUFLEN];
 //create a UDP socket
    if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
    {
            DieWithError("socket() failed");
    }

// zero out the structure
    memset((char *) &si_me, 0, sizeof(si_me));

    si_me.sin_family = AF_INET;
    si_me.sin_port = htons(PORT);
    si_me.sin_addr.s_addr = htonl(INADDR_ANY);

//bind socket to port
    if( bind(s , (struct sockaddr*)&si_me, sizeof(si_me) ) == -1)
    {
            DieWithError("bind() failed");
    }



//check digits or alpha



/////////////////////////////////////////////////////////
    len = recv(mysocket, buffer, MAXRCVLEN, 0);
    if(len < 0){
            perror("Reciver failed");
            close(mysocket);
            return EXIT_FAILURE;
    }
    else{
            buffer[len] = '\0';
            printf("I Received %s \n", buffer);
    }


            //printf("Received: %s ", buffer);


    while(1){
        memset(msg, 0, sizeof(msg));
            printf("Enter a character ");
            fgets(msg, sizeof(msg), stdin);
            if(strncmp(msg, "exit", 4) == 0) 
            ;
            if(send(mysocket, msg, strlen(msg), 0) < 0){
                    perror("Send failed");
                    close(mysocket);
                    return EXIT_FAILURE;
            }

        if ((recv_len = recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *) &si_other, &slen)) == -1 )//to receive message from server as UDP
            {
                DieWithError("recvfrom() failed");
            }
        printf("Received a packet from %s:%d\n", inet_ntoa(si_other.sin_addr), ntohs(si_other.sin_port)); 
        printf("Data: %s\n" , buf);
    }
 //printf("Received: %s ", buffer);    
    close(mysocket);
    return EXIT_SUCCESS;
}