网络通讯三要素:源 目的 长度
数据传输: TCP 可靠 重传 UDP 不可靠 (视频通话)
相关函数
socket() bind() listen() accept () connect() send() recv()
server.c //可接受多个客户端请求(fork)
#include <sys/types.h>
#incldue <sys/socket.h>
#incldue <string.h>
#include <netinet/in.h>
#incldue <arpa/inet.h>
#define SERVER_PORT 8888
#define BACKLOG 10
int main()
{
int iSocketServer;
struct sockaddr_in tSocketServerAddr;
struct sockaddr_in tSocketClieAddr;
int iSocketClient;
int iRet;
int iAddrLen;
iSocketServer = socket(AP_INET, SOCK_STREAM, 0);
if (-1 == iSocketServer )
{
printf("socket error!
");
return -1;
}
tSocketServerAddr.sin_family = AF_INET;
tSocketServerAddr.sin_port = htons(SERVER_PORT); // host to net , short
tSocketServerAddr.sin_addr.s_addr = INADDR_ANY
memset(tSocketServerAddr.sin_zero, 0,8);
iRet = bind(iSocketServer, (const struct sockaddr *)&tSocketServerAddr, sizeof(struct sockaddr));
if ( -1 ==iRet)
{
printf("bind error!
");
return -1;
}
iRet = listen(iSocketServer, BACKLOG);
if (-1 == iRet)
{
printf("listen error!
");
return -1;
}
while (1)
{
iAddrLen = sizeof(struct sockaddr);
iSocketClient = accept( iSocketServer, (struct sockaddr *)&tSocketClieAddr);
if (-1 != iSocketClient)
{
priintf("Get connet from %s
", inet_ntoa())
}
}
return 0;
}