c语言 多线程套接字编程

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c语言 多线程套接字编程相关的知识,希望对你有一定的参考价值。

char * ip = (char *)malloc(sizeof(char) * 30);
printf("\033[18;3HEnter the Server's IP Address:\033[19;2H");
scanf("%s",ip);
Ccli.sin_family = AF_INET;
Ccli.sin_port = htons(DEFAULT_PORT);
Ccli.sin_addr.s_addr = inet_addr(ip);
cli_me.sin_family = AF_INET;
cli_me.sin_port = htons(DEFAULT_PORT);
cli_me.sin_addr.s_addr = INADDR_ANY;
cClient = socket(AF_INET, SOCK_STREAM, 0);
memset(cbuf, 0, sizeof(cbuf));
setsockopt(cClient, SOL_SOCKET, SO_REUSEADDR, &cint, sizeof(cint));
if(cClient < 0)

printf("socket() failure!\n");
exit(1);

bind(cClient, (struct sockaddr*)&cli_me, sizeof(cli_me));
printf("\033[1CConnecting IP[%s]\n",inet_ntoa(Ccli.sin_addr));
if(connect(cClient, (struct sockaddr*)&Ccli, sizeof(Ccli)) < 0)

printf("\033[1Cconnect() failure!\n");
单独运行这个线程时是没有问题的,但是当先茶创建另一个线程accept()阻塞后再运行这个线程,就会跳过让你输入ip,直接连接255.255.255.255请问是为什么
我只把出问题部分的代码列出来了...其他的好的代码都省略了,没有问题

参考技术A #include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/wait.h>
#include <sys/socket.h>

#define PORT 5000 // The port which is communicate with server
#define BACKLOG 10
#define LENGTH 512 // Buffer length
int main ()
int sockfd; // Socket file descriptor
int nsockfd; // New Socket file descriptor
int num;
int sin_size; // to store struct size
char sdbuf[LENGTH]; // Send buffer
struct sockaddr_in addr_local;
struct sockaddr_in addr_remote;
char sendstr[16]= "123456789 abcde";

/* Get the Socket file descriptor */
if( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1 )

printf ("ERROR: Failed to obtain Socket Despcritor.\n");
return (0);

else

printf ("OK: Obtain Socket Despcritor sucessfully.\n");


/* Fill the local socket address struct */
addr_local.sin_family = AF_INET; // Protocol Family
addr_local.sin_port = htons(PORT); // Port number
addr_local.sin_addr.s_addr = INADDR_ANY; // AutoFill local address
bzero(&(addr_local.sin_zero), 8); // Flush the rest of struct

/* Blind a special Port */
if( bind(sockfd, (struct sockaddr*)&addr_local, sizeof(struct sockaddr)) == -1 )

printf ("ERROR: Failed to bind Port %d.\n",PORT);
return (0);

else

printf("OK: Bind the Port %d sucessfully.\n",PORT);


/* Listen remote connect/calling */
if(listen(sockfd,BACKLOG) == -1)

printf ("ERROR: Failed to listen Port %d.\n", PORT);
return (0);

else

printf ("OK: Listening the Port %d sucessfully.\n", PORT);


while(1)

sin_size = sizeof(struct sockaddr_in);

/* Wait a connection, and obtain a new socket file despriptor for single connection */
if ((nsockfd = accept(sockfd, (struct sockaddr *)&addr_remote, &sin_size)) == -1)

printf ("ERROR: Obtain new Socket Despcritor error.\n");
continue;

else

printf ("OK: Server has got connect from %s.\n", inet_ntoa(addr_remote.sin_addr));


/* Child process */
if(!fork())

printf("You can enter string, and press 'exit' to end the connect.\n");
while(strcmp(sdbuf,"exit") != 0)

scanf("%s", sdbuf);
if((num = send(nsockfd, sdbuf, strlen(sdbuf), 0)) == -1)

printf("ERROR: Failed to sent string.\n");
close(nsockfd);
exit(1);

printf("OK: Sent %d bytes sucessful, please enter again.\n", num);


close(nsockfd);
while(waitpid(-1, NULL, WNOHANG) > 0);

参考技术B 你要先bind再listen再accept
你服务端 的顺序、流程不对
参考技术C 你ip的初始值是多少?
有没有重定向过标准输入?
另一个线程是否也阻塞在读标准输入上?

以上是关于c语言 多线程套接字编程的主要内容,如果未能解决你的问题,请参考以下文章

linux套接字编程之多进程多线程练习

Windows 上的 C 中的套接字编程(服务器、使用 select() 和 fd_set 的多线程)

多线程编程

ZMQ之多线程编程

Linux篇第十九篇——网络套接字编程(TCP套接字的编写+多进程版本+多线程版本+线程池版本)

Linux篇第十九篇——网络套接字编程(TCP套接字的编写+多进程版本+多线程版本+线程池版本)