#include <sys/types.h> #include<sys/socket.h> #include<stdio.h> #include<string.h> #include<netinet/in.h> #include <unistd.h> #include <stdlib.h> #include <pthread.h> #define PORT 6000; #define SERVER_IP "192.168.40.128" void *routine(void * arg) { int newsockfd=(int *)&arg; char buf[10]; while(1) { bzero(buf,10); int size=recv(newsockfd,buf,sizeof(buf),0); buf[size]='\0'; printf("recive from client is : %s",buf); } } int main() { char buf[10]="hello"; int sockfd=socket(AF_INET,SOCK_STREAM,0); if(sockfd<0) { perror("socket fail\n"); return -1; } //Set Sockopt int sinsize = 1; int ret = setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &sinsize, sizeof(int)); if(ret != 0) { perror("Set sockopt fail!\n"); exit -1; } struct sockaddr_in s; memset(&s,0,sizeof(s)); s.sin_family=AF_INET; s.sin_port=htons(6000); s.sin_addr.s_addr=inet_addr("192.168.40.128");// 要 求 大 端模式的端口号和 IP 地址 int bi=bind(sockfd,(struct sockaddr *)&s,sizeof(struct sockaddr)); if(bi<0) { perror("bind fail\n"); } listen(sockfd,5); struct sockaddr_in c; int size=sizeof(struct sockaddr); int newsockfd=accept(sockfd,(struct sockaddr *)&c,&size); /********************************** 创 建 线 程 ********************************************/ pthread_t pid; pthread_create(&pid,NULL,routine,(void *)socket); while(1) { memset(buf,0,10); fgets(buf,10,stdin); int slen=send(newsockfd,buf,strlen(buf),0); if(slen<0) { printf("send failed\n"); return -1; } } pthread_join(pid,NULL); close(newsockfd); close(sockfd); return 0; }
时间: 2023-08-01 15:09:59 浏览: 155
Linux下socket通信.docx
这段代码存在一些错误。首先,在 `#define PORT 6000;` 中应该去掉分号,否则会导致端口号变成 6001。其次,在 `exit -1;` 中应该使用括号,即 `exit(-1);`。最后,在创建线程时,应该将 `socket` 改成 `newsockfd`,即 `pthread_create(&pid,NULL,routine,(void *)newsockfd);`。修改后的代码如下:
```c
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <string.h>
#include <netinet/in.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#define PORT 6000
#define SERVER_IP "192.168.40.128"
void *routine(void *arg)
{
int newsockfd = *(int *)arg;
char buf[10];
while(1)
{
bzero(buf, 10);
int size = recv(newsockfd, buf, sizeof(buf), 0);
buf[size] = '\0';
printf("recive from client is : %s", buf);
}
}
int main()
{
char buf[10] = "hello";
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
if(sockfd < 0)
{
perror("socket fail\n");
return -1;
}
// Set Sockopt
int sinsize = 1;
int ret = setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &sinsize, sizeof(int));
if(ret != 0)
{
perror("Set sockopt fail!\n");
exit(-1);
}
struct sockaddr_in s;
memset(&s, 0, sizeof(s));
s.sin_family = AF_INET;
s.sin_port = htons(PORT);
s.sin_addr.s_addr = inet_addr(SERVER_IP);
int bi = bind(sockfd, (struct sockaddr *)&s, sizeof(struct sockaddr));
if(bi < 0)
{
perror("bind fail\n");
return -1;
}
listen(sockfd, 5);
struct sockaddr_in c;
int size = sizeof(struct sockaddr);
int newsockfd = accept(sockfd, (struct sockaddr *)&c, &size);
/********************************** 创建线程 ********************************************/
pthread_t pid;
pthread_create(&pid, NULL, routine, (void *)&newsockfd);
while(1)
{
memset(buf, 0, 10);
fgets(buf, 10, stdin);
int slen = send(newsockfd, buf, strlen(buf), 0);
if(slen < 0)
{
printf("send failed\n");
return -1;
}
}
pthread_join(pid, NULL);
close(newsockfd);
close(sockfd);
return 0;
}
```
阅读全文