Linux下C语言socket编程

Server.c


#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

int main(void) {
printf("Server is running \n");
// 地址
struct sockaddr_in sockAddr;
    /** PROTO: int socket(int domain, int type, int protocol);
* domain: AF_INET   ==> ipv5 
*         AF_INET6  ==> ipv6
*         AF_UNIX   ==> 本地套接字(使用一个文件)
* type: SOCK_STREAM (可靠的面向服务或流套接字)
*       SOCK_DGEAM  (数据报文服务或数据报文套接字)
*       SOCK_SEQPACKET (可靠的连续数据包服务)
*       SOCK_RAW (在网络层之上的原始协议)
* protocol: 指定实际使用的传输协议。
*           IPPORTO_TCP, IPPROTO_SCTP, IPPROTO_UDP, IPPROTO_DCCP 等
*           如果是"0", 根据选定的domain和type选择使用缺省协议。
* 如果发生错误,函数返回值为-1。 否则,函数会返回一个代表新分配的描述符的整数。
*/ 
int socketFD = socket(AF_INET, SOCK_STREAM, 0);
if (-1 == socketFD) {
perror("Cannot create socket");
exit(EXIT_FAILURE);
}
printf(" Create socket success! \n");
memset(&sockAddr, 0, sizeof(struct sockaddr_in));

sockAddr.sin_family = AF_INET;
sockAddr.sin_port = htons(1100);
sockAddr.sin_addr.s_addr = INADDR_ANY;

int bind_res = bind(socketFD, (const struct sockaddr *)&sockAddr, sizeof(struct sockaddr_in));
if (-1 == bind_res) {
perror(" Error bind failed!");
close(socketFD);
exit(EXIT_FAILURE);
}
printf("Bind success ! \n");
if (-1 == listen(socketFD, 10)){
perror("Error Listen failed!");
close(socketFD);
exit(EXIT_FAILURE);
}
printf("Listen the port ... \n");
for (;;) {
int connectFD = accept(socketFD, NULL, NULL);
if (0 > connectFD) {
perror("Error Accept failed!");
close(socketFD);
exit(EXIT_FAILURE);
}
printf("Accept one client ... \n");
char buf[15];
ssize_t rval = recv(connectFD, buf, 15, 0);
printf("Receive from client... \n");
if (rval < 0) {
perror("Error Receive failed!");
shutdown(connectFD, SHUT_RDWR);
close(connectFD);
close(socketFD);
exit(EXIT_FAILURE);
} 
if (rval == 0) {
printf("Ending connection \n");
}
else {
printf("Received from client is: \n");
printf("The buf is %s", buf);
}
shutdown(connectFD, SHUT_RDWR);
close(connectFD);
}
close(socketFD);
return 0;
}


Client.c


#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

int main(void) {

struct sockaddr_in sockAddr;
int res;
int socketFD = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (-1 == socketFD) {
perror ("Cannot create socket");
exit(EXIT_FAILURE);
}

memset(&sockAddr, 0, sizeof(struct sockaddr_in));

sockAddr.sin_family = AF_INET;
sockAddr.sin_port = htons(1100);

res = inet_pton(AF_INET, "192.168.0.101", &sockAddr.sin_addr);

if (0 > res) {
perror ("Error: first parameter is not a valid address family");
close(socketFD);
exit(EXIT_FAILURE);
} else if (0 == res) {
perror ("Error: Secend parameter does not contain valid ip");
close(socketFD);
exit(EXIT_FAILURE);
}
if (-1 == connect(socketFD, (const struct sockaddr *)&sockAddr, sizeof(struct sockaddr_in))) {
perror("Error Cannot connect to server!");
close(socketFD);
exit(EXIT_FAILURE);
}
char buf[15] = "Hello World\n";
if (0 > send(socketFD, buf, 15, 0)) {
perror("Error Cannot send on stream socket");
close(socketFD);
exit(EXIT_FAILURE);
}
shutdown(socketFD, SHUT_RDWR);
printf("Send data is: %s", buf);
close(socketFD);
return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值