Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 插座C++;_C++_Sockets - Fatal编程技术网

C++ 插座C++;

C++ 插座C++;,c++,sockets,C++,Sockets,我设置了一个客户端和服务器来相互通信。但每次我尝试回显到客户端时,套接字似乎已断开连接。大部分代码改编自上一篇sockets教程。另外,我正在通过ssh远程运行这个 客户: 服务器输出: 如果未指定MSG_NOSIGNAL标志,服务器将在send()崩溃,这意味着套接字已在另一端断开连接。为什么套接字在send()/recv()对之后总是断开连接 我为我提交的内容的可读性/风格/纯粹的愚蠢表示歉意 谢谢你的帮助 在服务器中,您正在使用: if ((bytes_sent = send(sock,

我设置了一个客户端和服务器来相互通信。但每次我尝试回显到客户端时,套接字似乎已断开连接。大部分代码改编自上一篇sockets教程。另外,我正在通过ssh远程运行这个

客户: 服务器输出: 如果未指定MSG_NOSIGNAL标志,服务器将在send()崩溃,这意味着套接字已在另一端断开连接。为什么套接字在send()/recv()对之后总是断开连接

我为我提交的内容的可读性/风格/纯粹的愚蠢表示歉意


谢谢你的帮助 在服务器中,您正在使用:

 if ((bytes_sent = send(sock, buf, rc, MSG_NOSIGNAL)) < 0) {
         cout << "error sending\n";
         close(sock);
         exit(EXIT_FAILURE);
     }
if((字节发送=发送(sock、buf、rc、MSG\u NOSIGNAL))<0){
库特
#include <iostream>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdlib.h>
#include <strings.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <cstring>
#define MAXHOSTNAME 256

using namespace std;

main(int argc, char *argv[])
{
    if (argc != 2) {
        cout << "not enough arguments, ex: ./CaesarCipherServer 9876\n";
        exit(EXIT_FAILURE);
    }

    struct sockaddr_in socketInfo;
    char sysHost[MAXHOSTNAME+1]; // Hostname of this computer we're running on
    struct hostent *hPtr;
    int portNumber = atoi(argv[1]);
    int sock;

    bzero(&socketInfo, sizeof(sockaddr_in)); // Clear structure memory

    // Get system information
    gethostname(sysHost, MAXHOSTNAME); // Get this computer's hostname

    if ((hPtr = gethostbyname(sysHost)) == NULL)
    {
        cerr << "System hostname misconfigured." << endl;
        exit(EXIT_FAILURE);
    }

    if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
    {
        close(sock);
        exit(EXIT_FAILURE);
    }

    // Load system info into socket data structures

    socketInfo.sin_family = AF_INET;
    socketInfo.sin_addr.s_addr = htonl(INADDR_ANY); // Use any addr available
    socketInfo.sin_port = htons(portNumber); // Set port number

    // Bind the socket to a local socket address

     if (bind(sock, (struct sockaddr *) &socketInfo, sizeof(socketInfo)) < 0)
     {
         close(sock);
         perror("bind");
         exit(EXIT_FAILURE);
     }

     cout << "listening for initial connection \n";
     listen(sock, 1);

     int sockConn;
     if ((sockConn = accept(sock, NULL, NULL)) < 0)
     {
         exit(EXIT_FAILURE);
     }  else {
         cout << "connection accepted!\n";
     }

     int rc = 0;
     char buf[512];

     cout << "about to receive message... \n";
     // rc is number of chars returned
     rc = recv(sockConn, buf, 512, 0);
     buf[rc] = (char)NULL; // Null terminate string

     cout << "received: " << buf << endl;
     cout << "rc: " << rc << endl;


     int bytes_sent;
     if ((bytes_sent = send(sock, buf, rc, MSG_NOSIGNAL)) < 0) {
         cout << "error sending\n";
         close(sock);
         exit(EXIT_FAILURE);
     }

     cout << "bytes sent: " << bytes_sent << endl;

     close(sock);
}
./CaesarCipherClient cs-ssh 9876
Welcome!
Socket created!
socket handle : 3
Connected!
Please indicate rotation amount:5
bytes sent: 2
received: 
bytes received: 0
Please indicate rotation amount:
./CaesarCipherServer 9876
listening for initial connection 
connection accepted!
about to receive message... 
received: 5
rc: 2
error sending
 if ((bytes_sent = send(sock, buf, rc, MSG_NOSIGNAL)) < 0) {
         cout << "error sending\n";
         close(sock);
         exit(EXIT_FAILURE);
     }