当前位置: 技术问答>linux和unix
sock编程出错,求助!
来源: 互联网 发布时间:2016-04-11
本文导语: /******发送带外数据客户端**********/ //#include //#include //#include #include #include #include #define SERVE_PORT 8003 int main(int argc, char **argv) { int sockfd; int conn_flag; int num_bytes; struct sockaddr_in serv_addr; struct hostent *h; if(argc...
/******发送带外数据客户端**********/
//#include
//#include
//#include
#include
#include
#include
#define SERVE_PORT 8003
int main(int argc, char **argv)
{
int sockfd;
int conn_flag;
int num_bytes;
struct sockaddr_in serv_addr;
struct hostent *h;
if(argc != 2)
{
fprintf(stderr, "parameter error!n");
exit(1);
}
if(sockfd = socket(AF_INET, SOCK_STREAM, 0) h_addr);
if((conn_flag = connect(sockfd, (struct sockaddr*)&serv_addr, sizeof(struct s
ockaddr))) == -1)
{
fprintf(stderr, "connetc error:%sn");
exit(1);
}
if((num_bytes = send(sockfd, "abcd", 4, 0)) == -1)
{
fprintf(stderr, "send normal data abcd error!n");
exit(1);
}
sleep(2);
if((num_bytes = send(sockfd, "E", 1, MSG_OOB)) == -1)
{
fprintf(stderr, "send out-of-band data E error!n");
exit(1);
}
sleep(2);
if((num_bytes = send(sockfd, "fghi", 4, 0)) == -1)
{
fprintf(stderr, "send normal data fghi error");
exit(1);
}
sleep(2);
if((num_bytes = send(sockfd, "JK", 1, MSG_OOB)) == -1)
{
fprintf(stderr, "send out-of-band data JK error!n");
exit(1);
}
sleep(2);
if((num_bytes = send(sockfd, "I", 1, 0)) == -1)
{
fprintf(stderr, "send normal data I error!n");
exit(1);
}
close(sockfd);
}
这个运行时提示段错误。
/*******通过信号SIGURG接收带外数据*********/
#include
#include
#include
#include
#include
#include
#include
#define SERVE_PORT 8003
#define LISTENQUEUE 1
#define MSG_BUF_SIZE 512
int conn_fd;
void handle_signal(int signal)
{
int num_bytes;
char outofband_data;
if((num_bytes = recv(conn_fd, &outofband_data, 1, MSG_OOB))
//#include
//#include
//#include
#include
#include
#include
#define SERVE_PORT 8003
int main(int argc, char **argv)
{
int sockfd;
int conn_flag;
int num_bytes;
struct sockaddr_in serv_addr;
struct hostent *h;
if(argc != 2)
{
fprintf(stderr, "parameter error!n");
exit(1);
}
if(sockfd = socket(AF_INET, SOCK_STREAM, 0) h_addr);
if((conn_flag = connect(sockfd, (struct sockaddr*)&serv_addr, sizeof(struct s
ockaddr))) == -1)
{
fprintf(stderr, "connetc error:%sn");
exit(1);
}
if((num_bytes = send(sockfd, "abcd", 4, 0)) == -1)
{
fprintf(stderr, "send normal data abcd error!n");
exit(1);
}
sleep(2);
if((num_bytes = send(sockfd, "E", 1, MSG_OOB)) == -1)
{
fprintf(stderr, "send out-of-band data E error!n");
exit(1);
}
sleep(2);
if((num_bytes = send(sockfd, "fghi", 4, 0)) == -1)
{
fprintf(stderr, "send normal data fghi error");
exit(1);
}
sleep(2);
if((num_bytes = send(sockfd, "JK", 1, MSG_OOB)) == -1)
{
fprintf(stderr, "send out-of-band data JK error!n");
exit(1);
}
sleep(2);
if((num_bytes = send(sockfd, "I", 1, 0)) == -1)
{
fprintf(stderr, "send normal data I error!n");
exit(1);
}
close(sockfd);
}
这个运行时提示段错误。
/*******通过信号SIGURG接收带外数据*********/
#include
#include
#include
#include
#include
#include
#include
#define SERVE_PORT 8003
#define LISTENQUEUE 1
#define MSG_BUF_SIZE 512
int conn_fd;
void handle_signal(int signal)
{
int num_bytes;
char outofband_data;
if((num_bytes = recv(conn_fd, &outofband_data, 1, MSG_OOB))