本文介绍了在网络中的服务器端打印你好的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用网络服务器端程序在服务器端打印Hello。

在客户端和服务器端编写程序。如果我们在客户端写你好,它必须在服务器端打印..



我已经完成了这样的程序



客户端



How would I print a "Hello" on server side using networking server side program.
Write a program in both client and server side. If we write hello in client side it must be print on server side..

I have done such program

Client Side

#include <stdio.h>      /* for printf() and fprintf() */
#include <sys/socket.h> /* for socket(), connect(), sendto(), and recvfrom() */
#include <arpa/inet.h>  /* for sockaddr_in and inet_addr() */
#include <stdlib.h>     /* for atoi() and exit() */
#include <string.h>     /* for memset() */
#include <unistd.h>     /* for close() */

#define ECHOMAX 255     /* Longest string to echo */

void PrintError_exit(char *errorMessage);  /* External error handling function */

int main(int argc, char *argv[])
{
    int sock;                        /* Socket descriptor */
    struct sockaddr_in echoServAddr; /* Echo server address */
    struct sockaddr_in fromAddr;     /* Source address of echo */
    unsigned short echoServPort;     /* Echo server port */
    unsigned int fromSize;           /* In-out of address size for recvfrom() */
    char *servIP;                    /* IP address of server */
    char *echoString;                /* String to send to echo server */
    char echoBuffer[ECHOMAX+1];      /* Buffer for receiving echoed string */
    int echoStringLen;               /* Length of string to echo */
    int respStringLen;               /* Length of received response */

    if (argc != 4)    /* Test for correct number of arguments */
    {
        fprintf(stderr,"Usage: %s <Server IP> <Echo Word> <Echo Port>\n", argv[0]);
        exit(1);
    }

    servIP = argv[1];           /* First arg: server IP address (dotted quad) */
    echoString = argv[2];       /* Second arg: string to echo */
    echoStringLen = strlen(echoString); /* Calculate the length for later use */

    echoServPort = atoi(argv[3]);  /* Third Arg: Use given port, if any */


    /* Create a datagram/UDP socket */
    if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0)

        PrintError_exit("socket() failed");



    /* Construct the server address structure */

    memset(&echoServAddr, 0, sizeof(echoServAddr));    /* Zero out structure */

    echoServAddr.sin_family = AF_INET;                 /* Internet addr family */

    echoServAddr.sin_addr.s_addr = inet_addr(servIP);  /* Server IP address */

    echoServAddr.sin_port   = htons(echoServPort);     /* Server port */



    /* Send the string to the server */

    if (sendto(sock, echoString, echoStringLen, 0, (struct sockaddr *)

               &echoServAddr, sizeof(echoServAddr)) != echoStringLen)

        PrintError_exit("sendto() sent a different number of bytes than expected");



    /* Recv a response */

    fromSize = sizeof(fromAddr);

    if ((respStringLen = recvfrom(sock, echoBuffer, ECHOMAX, 0,

         (struct sockaddr *) &fromAddr, &fromSize)) != echoStringLen)

        PrintError_exit("recvfrom() failed");



      /* null-terminate the received data */

    echoBuffer[respStringLen] = '\0';



  printf("Received: %s\n", echoBuffer);    /* Print the echoed arg */



    close(sock);

    exit(0);

}



void PrintError_exit(char *errorMsg)  /* External error handling function */

{

        fprintf(stderr,"Error:%s\n",errorMsg);

        exit(0);

}











服务器端








Server Side

#include <stdio.h>      /* for printf() and fprintf() */
#include <sys/socket.h> /* for socket() and bind() */
#include <arpa/inet.h>  /* for sockaddr_in and inet_ntoa() */
#include <stdlib.h>     /* for atoi() and exit() */
#include <string.h>     /* for memset() */
#include <unistd.h>     /* for close() */

#define ECHOMAX 255     /* Longest string to echo */

void PrintError_exit(char *);  /* External error handling function */


int main(int argc, char *argv[])
{
    int sock;                        /* Socket */
    struct sockaddr_in echoServAddr; /* Local address */
    struct sockaddr_in echoClntAddr; /* Client address */
    unsigned int cliAddrLen;         /* Length of incoming message */
    char echoBuffer[ECHOMAX];        /* Buffer for echo string */
    unsigned short echoServPort;     /* Server port */
    int recvMsgSize;                 /* Size of received message */

    if (argc != 2)         /* Test for correct number of parameters */
    {
        fprintf(stderr,"Usage:  %s <UDP SERVER PORT>\n", argv[0]);
        exit(1);
    }

    echoServPort = atoi(argv[1]);  /* First arg:  local port */

    /* Create socket for sending/receiving datagrams */
   // if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0)

    sock = socket(AF_INET, SOCK_DGRAM,IPPROTO_UDP);

    if (sock < 0)

        PrintError_exit("socket() failed");

    else

    printf("Socket Created Successfully");



    /* Construct local address structure */

    memset(&echoServAddr, 0, sizeof(echoServAddr));   /* Zero out structure */

    echoServAddr.sin_family = AF_INET;                /* Internet address family */

    echoServAddr.sin_addr.s_addr = htonl(INADDR_ANY); /* Any incoming interface */

    echoServAddr.sin_port = htons(echoServPort);      /* Local port */



    /* Bind to the local address */

    if (bind(sock, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr)) < 0)

        PrintError_exit("bind() failed");



    for (;;) /* Run forever */

    {

        /* Set the size of the in-out parameter */

        cliAddrLen = sizeof(echoClntAddr);



        /* Block until receive message from a client */

        if ((recvMsgSize = recvfrom(sock, echoBuffer, ECHOMAX, 0,

            (struct sockaddr *) &echoClntAddr, &cliAddrLen)) < 0)

            PrintError_exit("recvfrom() failed");



/* Know the IP address of the client, from where the request is originated */

        printf("Handling client %s\n", inet_ntoa(echoClntAddr.sin_addr));

/* Received data from client */

    echoBuffer[recvMsgSize]='\0';

    printf("Data received %s \n", echoBuffer);



        /* Send received datagram back to the client */

        if (sendto(sock, echoBuffer, recvMsgSize, 0,

             (struct sockaddr *) &echoClntAddr, sizeof(echoClntAddr)) != recvMsgSize)

            PrintError_exit("sendto() sent a different number of bytes than expected");

    }

    /* NOT REACHED */

}





void PrintError_exit(char *errorMsg)  /* External error handling function */

{

    fprintf(stderr,"Error:%s\n",errorMsg);

    exit(0);

}

推荐答案

printf("Hello");







Now you have added some code it seems this is a C program, but it is no more clear what your problem is.




Now you have added some code it seems this is a C program, but it is no more clear what your problem is.


这篇关于在网络中的服务器端打印你好的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 23:09
查看更多