socket简单通信

粗糙简略的初版,后续多加点功能权当练手
/*
============================================================================
Name : server.c
Author : king
Version :
Copyright : Your copyright notice
Description : Hello World in C, Ansi-style
============================================================================
*/
#include <stdlib.h>
#include <pthread.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h> /* inet(3) functions */ #include <stdlib.h>
#include <errno.h>
#include <stdio.h>
#include <string.h> int handle(int point); int main(void) {
int sfd, ind;
struct sockaddr_in addr;
struct sockaddr_in clent;
char resv[], sendbuf[];
char buf[];
char * myaddr = "192.168.231.128"; int ret; // 返回值设置
socklen_t lent;
int pid;
addr.sin_family = AF_INET; //IPv4 Internet protocols addr.sin_port = htons(); //这里输入服务器端口号 addr.sin_addr.s_addr = inet_addr(myaddr);
; //INADDR_ANY表示本机IP //獲取socket描述符,IPV4asd
printf("socket start \n");
sfd = socket(AF_INET, SOCK_STREAM, ); if (sfd < ) {
printf("socket error \n");
return -;
}
printf("bind start \n");
//将套接子与指定端口链接
if (bind(sfd, (struct sockaddr *) &addr, sizeof(struct sockaddr)) < ) {
printf("bind error \n");
return -;
} //监听套接子
printf("listen start \n");
if (listen(sfd, ) < ) {
printf("listen error \n");
return -;
} for (;;) {
//接受来自客户端的信息
printf("accept start \n");
memset(&clent, , sizeof(clent));
lent = sizeof(clent);
ind = accept(sfd, (struct sockaddr *) &clent, &lent);
if (ind < ) {
printf("accept error %d \n", ind);
return -;
} printf("infor \n");
printf("clent addr%s porit %d\n",
inet_ntop(AF_INET, &clent.sin_addr, buf, sizeof(buf)),
ntohs(clent.sin_port)); pid = fork(); if (pid == ) {
//子进程
close(sfd);
handle(ind);
} else if (pid < ) {
//error
close(ind);
} else {
//父进程
}
} return EXIT_SUCCESS; } int handle(int point) { int retn;
char buf[]; for (;;) {
retn = read(point, buf, sizeof(buf));
if (retn < ) {
printf("read error \n");
close(point);
break;
} else if (retn == ) {
printf("client exit \n");
close(point);
break;
} printf("client:%s\n", buf); if (strcmp("exit", buf) == ) {
printf("exit \n");
close(point);
return ;
}
}
return ;
}
/*
============================================================================
Name : client.c
Author : king
Version :
Copyright : Your copyright notice
Description : Hello World in C, Ansi-style
============================================================================
*/ #include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h> /* inet(3) functions */ int handle(int fd); int main(void) { int nsd;
char buf[]; char * myaddr = "192.168.231.128";
struct sockaddr_in addr; printf("welcome to echo client\n");
nsd = socket(AF_INET, SOCK_STREAM, );
printf("connect start1 \n");
//bzero(addr, sizeof(*addr));
memset(&addr,,sizeof(addr));
printf("connect start2 \n");
addr.sin_family = AF_INET;
addr.sin_port = htons();
addr.sin_addr.s_addr=inet_addr(myaddr); printf("connect start3 \n");
if (connect(nsd, (struct sockaddr *)&addr, sizeof(struct sockaddr)) < ) {
printf("connect error \n");
return -;
} sleep();
printf("handle start\n");
handle(nsd);
close(nsd);
return EXIT_SUCCESS;
} int handle(int fd) { char sendl[], rev[]; int retn; for (;;) { memset(sendl,,sizeof(sendl));
memset(rev,,sizeof(rev));
if (fgets(sendl, , stdin) == NULL) {
break;
}
//
printf("wirte start\n");
write(fd, sendl, strlen(sendl));
read(fd, rev,strlen(rev)); } return ;
}

虽然写程序写了好几年,感觉还是技术不过关,于是把socket从新复习了下,
期间遇到错误一直没有编译过去:
connect和accept
因为没有深刻了解这两函数的参数,所以酿成打错

int connect(int sockfd, const struct sockaddr *addr,
socklen_t addrlen);

记住一定是值 addrlen

accept   socklen_t *addrlen要是一个指针

这俩个郁闷死我了,我以为是一样的,结果调了1小时。

还需努力啊。

上一篇:How to setup SLF4J and LOGBack in a web app - fast--转载


下一篇:设计模式的征途—15.观察者(Observer)模式