当前位置: 技术问答>linux和unix
Linux 父子进程使用TCP连接通信connect错误
来源: 互联网 发布时间:2016-07-21
本文导语: 函数功能是父子进程使用TCP socket进行通信,只需要把字符串显示出来,运行提示错误是 at line 176 connect ERROR: con_rtn = -1, errno = 22 Invalid argument 不知何故? /* *function : TCP communication with a forked subtask ...
函数功能是父子进程使用TCP socket进行通信,只需要把字符串显示出来,运行提示错误是
at line 176 connect ERROR: con_rtn = -1, errno = 22 Invalid argument
不知何故?
/*
*function : TCP communication with a forked subtask
struct sockaddr_un
{
sa_family_t sun_family; // PF_NUIX or AF_UNIX or AF_LOCAL
char sun_path[UNIX_PATH_MAX]; //name of path;
};
//usr/include/linux/socket.h
typedef unsigned short sa_family_t;
struct sockaddr //this is general structure;
{
unsigned short sa_family; //AF_xxx
char sa_data[14]; // 14 bytes of protocol address
};
//usr/include/netinet/in.h
struct sockaddr_in //internet socket, can convert between above and it;
{
short int sin_family; //address cluster
unsigned short int sin_port; //port number
struct in_addr; //ip address
unsigned char sin_zero[8]; //for the same size as struct sockaddr
};
struct in_addr
{
unsigned long s_addr;
};
inet_addr()is used to convert general ip 192.168.0.1 int 32bits ip 0xc0a80001;
// return actual bytes been sent. flag always been 0, if error errno will be set;
int send(int sockfd, const void* msg, int len, int flags);
// use unlinked diagram to send message, so need sockaddr to define ip and port, in general, tolen is sizeof(struct sockaddr), and return value as above;
int sendto(int sockfd, const void* msg, int len, unsigned int flags, const struct sockaddr* to, int tolen);
//
int recv(int sockfd, void* buf, int len, unsigned int flags);
//
int recvfrom(int sockfd, void* buf, int len, unsigned int flags, struct sockaddr* from, int*fromlen);
once you use diagram socket, but use connect, you can use send() and recv() too, but this socket will still use diagram for service in transport layer, but in send or receive, kernel will add source and destination address for it;
close(sockfd) or int shutdown(int sockfd, int how) to end a data transport;
*create time : 2009/12/07 13:50
*modify time :
*history :
*/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
extern int errno;
extern char* strerro(int );
char path[] = {"/tmp/socketmy"}; /* socket name, */
int main(void)
{
struct sockaddr_un sock;
int len=sizeof(sock);
int pid; /* child task's process id */
int soc; /* socket file descriptor */
int con_soc;
int lisn_rtn;
int bind_rtn;
int con_rtn;
int com_soc;
char buffer[80];
errno = 0;
memset((char *) &sock, 0, sizeof(sock));
strcpy(sock.sun_path, path);
sock.sun_family = AF_UNIX;
unlink(path);
/* establish and initialize TCP socket struct */
soc = socket(AF_UNIX, SOCK_STREAM, 0);
if (soc
at line 176 connect ERROR: con_rtn = -1, errno = 22 Invalid argument
不知何故?
/*
*function : TCP communication with a forked subtask
struct sockaddr_un
{
sa_family_t sun_family; // PF_NUIX or AF_UNIX or AF_LOCAL
char sun_path[UNIX_PATH_MAX]; //name of path;
};
//usr/include/linux/socket.h
typedef unsigned short sa_family_t;
struct sockaddr //this is general structure;
{
unsigned short sa_family; //AF_xxx
char sa_data[14]; // 14 bytes of protocol address
};
//usr/include/netinet/in.h
struct sockaddr_in //internet socket, can convert between above and it;
{
short int sin_family; //address cluster
unsigned short int sin_port; //port number
struct in_addr; //ip address
unsigned char sin_zero[8]; //for the same size as struct sockaddr
};
struct in_addr
{
unsigned long s_addr;
};
inet_addr()is used to convert general ip 192.168.0.1 int 32bits ip 0xc0a80001;
// return actual bytes been sent. flag always been 0, if error errno will be set;
int send(int sockfd, const void* msg, int len, int flags);
// use unlinked diagram to send message, so need sockaddr to define ip and port, in general, tolen is sizeof(struct sockaddr), and return value as above;
int sendto(int sockfd, const void* msg, int len, unsigned int flags, const struct sockaddr* to, int tolen);
//
int recv(int sockfd, void* buf, int len, unsigned int flags);
//
int recvfrom(int sockfd, void* buf, int len, unsigned int flags, struct sockaddr* from, int*fromlen);
once you use diagram socket, but use connect, you can use send() and recv() too, but this socket will still use diagram for service in transport layer, but in send or receive, kernel will add source and destination address for it;
close(sockfd) or int shutdown(int sockfd, int how) to end a data transport;
*create time : 2009/12/07 13:50
*modify time :
*history :
*/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
extern int errno;
extern char* strerro(int );
char path[] = {"/tmp/socketmy"}; /* socket name, */
int main(void)
{
struct sockaddr_un sock;
int len=sizeof(sock);
int pid; /* child task's process id */
int soc; /* socket file descriptor */
int con_soc;
int lisn_rtn;
int bind_rtn;
int con_rtn;
int com_soc;
char buffer[80];
errno = 0;
memset((char *) &sock, 0, sizeof(sock));
strcpy(sock.sun_path, path);
sock.sun_family = AF_UNIX;
unlink(path);
/* establish and initialize TCP socket struct */
soc = socket(AF_UNIX, SOCK_STREAM, 0);
if (soc