如何解决套接字编程错误?

时间:2015-09-27 07:20:39

标签: c++ sockets

我正在实现机器问题而且我遇到了错误,我知道问题是什么,我想将命令行参数发送到sendto()但是它给了我错误请提供给我我知道问题出在sendto()的参数中......以下是客户端程序:

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


using namespace std;

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

{

char msg[100];
int i=0;

char * adress;
char * subject;
char * name;

adress=argv[i+1];
subject=argv[i+2];
name=argv[i+3];

if(argc>9){
cout << "Just provide three arguments in commandline,please. " << endl;
}



int conn_sock,n,err;
struct sockaddr_in server_addr;
conn_sock=socket(AF_INET,SOCK_DGRAM,0);

if(conn_sock ==-1)
{
perror("\n\nError in making socket and error is");

cout<<"Error No:\t\n"<<errno;
exit(0);
}


server_addr.sin_family= AF_INET;
server_addr.sin_port = 1234;
server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");


int p=sizeof(server_addr);
//const void*  a =( const void*(strlen(adress)));
n=sendto(conn_sock,msg,0,(sockaddr*)&server_addr,p);

exit(0);
}

1 个答案:

答案 0 :(得分:0)

您正在使用sendto,如下所示:

n = sendto( conn_sock, msg, 0, (sockaddr*)&server_addr, p );

您错过了上述通话的flags参数。请参阅sendto documentation

ssize_t sendto(int sockfd, const void *buf, size_t len, int flags,
               const struct sockaddr *dest_addr, socklen_t addrlen);