赞
踩
tcp客户端
#include <stdio.h> #include <sys/types.h> #include <sys/socket.h> #include <arpa/inet.h> #include <unistd.h> #include <string.h> #define CLIENT_IP "192.168.8.101" //本机 IP #define SERVER_IP "192.168.8.101" //服务器 IP #define SERVER_PORT 8888 //端口号 #define CLIENT_PORT 6666 #define MAX 128 #define ERROR(msg) do{\ fprintf(stderr, "__%d__", __LINE__);\ perror(msg);\ }while(0) int main(int argc, char *argv[]) { int cfd; struct sockaddr_in client_addr,server_addr; socklen_t server_addr_size; //1.创建流式套接字 cfd = socket(AF_INET, SOCK_STREAM, 0); if (cfd < 0) { ERROR("socket"); return -1; } memset(&server_addr, 0, sizeof(struct sockaddr_in)); /* //bind操作可有可无 int sfd; memset(&client_addr, 0, sizeof(struct sockaddr_in)); //填充客户端地址信息结构体 client_addr.sin_family = AF_INET; client_addr.sin_port = htons(CLIENT_PORT); //端口的网络字节序 client_addr.sin_addr.s_addr = inet_addr(CLIENT_IP); //IP的网络字节序 //允许端口快速被重用 int reuse = 1; if(setsockopt(cfd, SOL_SOCKET, SO_REUSEADDR, &reuse ,sizeof(reuse)) < 0) { ERROR("setsockopt"); return -1; } //2.将地址信息绑定到套接字上 if (bind(cfd, (struct sockaddr *) &client_addr, sizeof(struct sockaddr_in)) < 0) { ERROR("bind"); return -1; } */ //填充服务器地址信息结构体 server_addr.sin_family = AF_INET; server_addr.sin_port = htons(SERVER_PORT); //端口的网络字节序 server_addr.sin_addr.s_addr = inet_addr(SERVER_IP); //IP的网络字节序 //3.连接服务器 server_addr_size = sizeof(struct sockaddr_in); if(connect(cfd, (struct sockaddr*)&server_addr, server_addr_size) < 0) { ERROR("connect"); return -1; } printf("服务器连接成功\n"); char buf[MAX] = ""; ssize_t res = 0; while(1) { fgets(buf, sizeof(buf), stdin); buf[strlen(buf)-1] = '\0'; //5.发送信息 if(send(cfd, buf, sizeof(buf), 0) < 0) { ERROR("send"); return -1; } //6.接收信息 res = recv(cfd, buf, sizeof(buf), 0); if(res < 0) { ERROR("recv"); return -1; } if(0 == res) { printf("客户端已关闭\n"); break; } printf("[%s:%d] recmsg:%s\n", \ inet_ntoa(server_addr.sin_addr), \ ntohs(server_addr.sin_port), buf); } close(cfd); }
tcp服务端
#include <stdio.h> #include <sys/types.h> #include <sys/socket.h> #include <arpa/inet.h> #include <unistd.h> #include <string.h> #define IP "192.168.8.101" //服务器 IP #define PORT 8888 //端口号 #define MAX 128 #define ERROR(msg) do{\ fprintf(stderr, "__%d__", __LINE__);\ perror(msg);\ }while(0) int main(int argc, char *argv[]) { int sfd, cfd; struct sockaddr_in server_addr, client_addr; socklen_t client_addr_size; //1.创建流式套接字 sfd = socket(AF_INET, SOCK_STREAM, 0); if (sfd < 0) { ERROR("socket"); return -1; } memset(&server_addr, 0, sizeof(struct sockaddr_in)); memset(&client_addr, 0, sizeof(struct sockaddr_in)); //填充地址信息结构体 server_addr.sin_family = AF_INET; server_addr.sin_port = htons(PORT); //端口的网络字节序 server_addr.sin_addr.s_addr = inet_addr(IP); //IP的网络字节序 //允许端口快速被重用 int reuse = 1; if(setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse ,sizeof(reuse)) < 0) { ERROR("setsockopt"); return -1; } //2.将地址信息绑定到套接字上 if (bind(sfd, (struct sockaddr *) &server_addr, sizeof(struct sockaddr_in)) < 0) { ERROR("bind"); return -1; } //3.将sfd设置为被动监听状态 if (listen(sfd, 128) < 0) { ERROR("listen"); return -1; } printf("服务器正在监听....\n"); client_addr_size = sizeof(struct sockaddr_in); char buf[MAX] = ""; ssize_t res = 0; while(1) { //4.从队列头获取一个客户的信息 cfd = accept(sfd, (struct sockaddr *) &client_addr, &client_addr_size); if (cfd < 0) { ERROR("accept"); return -1; } printf("[%s:%d] cfd = %d 连接成功\n", \ inet_ntoa(client_addr.sin_addr), \ ntohs(client_addr.sin_port), cfd); while(1) { bzero(buf, sizeof(buf)); //5.接收来自客户端信息 res = recv(cfd, buf, sizeof(buf), 0); if(res < 0) { ERROR("recv"); return -1; } if(0 == res) { printf("客户端已关闭\n"); break; } printf("[%s:%d] recmsg:%s\n", \ inet_ntoa(client_addr.sin_addr), \ ntohs(client_addr.sin_port), buf); //6.发送来自客户端信息 if(send(cfd, buf, sizeof(buf), 0) < 0) { ERROR("send"); return -1; } } } close(sfd); close(cfd); return 0; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。