Linux基於TCP/IP簡單的客戶端、服務器通信程序實例


服務器端代碼:

/*************************************************************************
> File Name: server.c
> Author: fucang_zxx
> Mail: fucang_zxx@163.com
> Created Time: Sun 24 Jul 2016 03:45:21 PM CST
************************************************************************/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <arpa/inet.h>
#include <netinet/in.h>

const int port = 8080;
const char* ip = "192.168.64.128";

int main()
{
//創建套接字,即創建socket
int ser_sock = socket(AF_INET, SOCK_STREAM, 0);
if(ser_sock < 0)
{
//創建失敗
perror("socket");
return 1;
}

//綁定信息,即命名socket
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
//inet_addr函數將用點分十進制字符串表示的IPv4地址轉化為用網絡
//字節序整數表示的IPv4地址
addr.sin_addr.s_addr = inet_addr(ip);
if(bind(ser_sock, (struct sockaddr*)&addr, sizeof(addr)) < 0)
{
//命名失敗
perror("bind");
return 2;
}

//監聽socket
int listen_sock = listen(ser_sock, 5);
if(listen_sock < 0)
{
//監聽失敗
perror("listen");
return 3;
}

//接受連接
//系統調用從listen監聽隊列中接受一個連接
//int accept(int sockfd, struct sockaddr* addr, socklen_t* addrlen)
//sockfd參數是執行過listen系統調用的監聽socket;addr參數用來獲取被
//接受連接的遠端socket地址,該socket地址的長度由addrlen參數指出
//accept成功時返回一個新的連接socket,該socket唯一的標識了被接受
//的這個連接,服務器可通過讀寫該socket來與被接受連接對應的客戶端通信
struct sockaddr_in peer;
socklen_t peer_len;
char buf[1024];
int accept_fd = accept(ser_sock, (struct sockaddr*)&peer, &peer_len);

if(accept_fd < 0)
{
perror("accept");
return 4;
}
else
{
printf("connected with ip: %s and port: %d\n", inet_ntop(AF_INET,&peer.sin_addr, buf, 1024), ntohs(peer.sin_port));

}

while(1)
{
memset(buf, '\0', sizeof(buf));
ssize_t size = read(accept_fd, buf, sizeof(buf) - 1);
// printf("size = %d\n", size);
// printf("buf = %s\n", buf);
if(size > 0)
{
printf("client#: %s\n", buf);
}
else if(size == 0)
{
printf("read is done...\n");
break;
}
else
{
perror("read");
break;
}
printf("server please enter: ");
fflush(stdout);
size = read(0, buf, sizeof(buf) - 1);
if(size > 0)
{
buf[size - 1] = '\0';
}
else if(size == 0)
{
printf("read is done...\n");
break;
}
else
{
perror("read");
break;
}
write(accept_fd, buf, strlen(buf));
}
close(ser_sock);
return 0;
}


客戶端代碼:

/*************************************************************************
> File Name: client.c
> Author: fucang_zxx
> Mail: fucang_zxx@163.com
> Created Time: Sun 24 Jul 2016 03:45:21 PM CST
************************************************************************/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <arpa/inet.h>
#include <netinet/in.h>

const int port = 8080;
const char* ip = "192.168.64.128";

int main()
{
//創建套接字,即創建socket
int clt_sock = socket(AF_INET, SOCK_STREAM, 0);
if(clt_sock < 0)
{
//創建失敗
perror("socket");
return 1;
}

//綁定信息,即命名socket
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
//inet_addr函數將用點分十進制字符串表示的IPv4地址轉化為用網絡
//字節序整數表示的IPv4地址
addr.sin_addr.s_addr = inet_addr(ip);

//不需要監聽

//發起連接
// struct sockaddr_in peer;
socklen_t addr_len = sizeof(addr);
int connect_fd = connect(clt_sock, (struct sockaddr*)&addr, addr_len);
if(connect_fd < 0)
{
perror("connect");
return 2;
}
char buf[1024];

while(1)
{
memset(buf, '\0', sizeof(buf));
printf("client please enter: ");
fflush(stdout);
ssize_t size = read(0, buf, sizeof(buf) - 1);
if(size > 0)
{
buf[size - 1] = '\0';
}
else if(size == 0)
{
printf("read is done...\n");
break;
}
else
{
perror("read");
return 4;
}
// printf("client: %s\n", buf);
write(clt_sock, buf, strlen(buf));
size = read(clt_sock, buf, sizeof(buf));
if(size > 0)
{
buf[size] = '\0';
}
else if(size == 0)
{
printf("read is done...\n");
break;
}
else
{
perror("read");
return 5;
}
printf("server: %s\n", buf);
}
close(clt_sock);
return 0;
}


運行截圖:

客戶端:


服務器端:



注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2021 ITdaan.com