==========================服务端代码===========================================
-
#include <stdio.h>
-
#include <strings.h>
-
#include <unistd.h>
-
#include <sys/types.h>
-
#include <sys/socket.h>
-
#include <netinet/in.h>
-
#include <arpa/inet.h>
-
-
#define PORT 1234 //端口号
-
#define BACKLOG 1 //请求队列中的最大连接数
-
-
int main()
-
{
-
int listenfd, connectfd; //监听套接字描述符与连接套接字描述符
-
struct sockaddr_in server, client; //服务器端和客户端IPV4地址信息
-
socklen_t addrlen;
-
-
//使用socket()函数,产生TCP套接字
-
if((listenfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
-
{
-
perror("socket() error.");
-
exit(1);
-
}
-
-
int opt = 1;
-
//设置套接字选项,使用SO_REUSEADDR允许套接口和一个已经在使用中的地址绑定
-
setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
-
//初始化server套接字地址结构
-
bzero(&server, sizeof(server));
-
server.sin_family = AF_INET;
-
server.sin_port = htons(PORT);
-
server.sin_addr.s_addr = htonl(INADDR_ANY);
-
-
//使用bind()函数,将套接字与指定的协议地址绑定
-
if(bind(listenfd, (struct sockaddr *)&server, sizeof(server)) == -1)
-
{
-
perror("Bind() error");
-
exit(1);
-
}
-
-
//使用listen()函数,等待客户端的连接
-
if(listen(listenfd, BACKLOG) == -1)
-
{
-
perror("listen() error.\n");
-
exit(1);
-
}
-
-
addrlen = sizeof(client);
-
-
-
//不断监听客户端的请求
-
while(1)
-
{
-
//接受客户端的连接,客户端的地址信息放在client地址结构中
-
if((connectfd = accept(listenfd, (struct sockaddr *)&client, &addrlen)) == -1)
-
{
-
perror("accept() error\n");
-
exit(1);
-
}
-
printf("You got a connection from client's IP is %s, port is %d\n",
-
inet_ntoa(client.sin_addr), ntohs(client.sin_port));
-
//服务器端对连接的客户端发送一条信息
-
send(connectfd, "Welcome\n", 8, 0);
-
//关闭与客户端的连接
-
close(connectfd);
-
}
-
-
close(listenfd);
-
return 0;
-
}
==========================客户端代码===========================================
-
#include <stdio.h>
-
#include <unistd.h>
-
#include <string.h>
-
#include <sys/types.h>
-
#include <sys/socket.h>
-
#include <netinet/in.h>
-
#include <netdb.h>
-
-
#define PORT 1234
-
-
#define MAXDATASIZE 100 //数据缓冲区最大长度
-
-
int main(int argc, char *argv[])
-
{
-
int sockfd, num;
-
//数据缓冲区
-
char buf[MAXDATASIZE];
-
struct hostent *he;
-
//服务器IPv4地址信息
-
struct sockaddr_in server;
-
-
if(argc != 2)
-
{
-
printf("Usage: %s \n", argv[0]);
-
exit(1);
-
}
-
-
//通过gethostbyname()得到服务端的主机信息
-
if((he = gethostbyname(argv[1])) == NULL)
-
{
-
printf("gethostbyname() error\n");
-
exit(1);
-
}
-
-
//使用socket()创建套接字
-
if((sockfd= socket(AF_INET, SOCK_STREAM, 0)) == -1)
-
{
-
printf("socket() error\n");
-
exit(1);
-
}
-
-
//初始化server地址信息
-
bzero(&server, sizeof(server));
-
server.sin_family = AF_INET;
-
server.sin_port = htons(PORT);
-
server.sin_addr = *((struct in_addr *)he->h_addr);
-
-
//使用connect()函数来配置套接字,建立一个与TCP服务器的连接
-
if(connect(sockfd, (struct sockaddr *)&server, sizeof(server)) == -1)
-
{
-
printf("connect() error\n");
-
exit(1);
-
}
-
-
//使用recv()函数接收服务器发送的信息
-
if((num = recv(sockfd, buf, MAXDATASIZE, 0)) == -1)
-
{
-
printf("recv() error\n");
-
exit(1);
-
}
-
-
buf[num - 1] = '\0';
-
printf("server message: %s\n", buf);
-
//关闭连接
-
close(sockfd);
-
return 0;
-
}
阅读(9354) | 评论(0) | 转发(0) |