創(chuàng)建tcp服務(wù)
在虛擬機上新建tcp_server.c:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define SERVER_PORT 3861
#define LISENT_NUM 10
int main(int argc, char * argv[])
{
int sfd, cfd;
struct sockaddr_in clientaddr;
struct sockaddr_in serverAddr;
char buff[1024];
int size = sizeof(struct sockaddr);
pthread_t client_thread[LISENT_NUM];
if((sfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
perror("socket");
exit(-1);
}
memset(&serverAddr, 0, sizeof(struct sockaddr));
serverAddr.sin_family = AF_INET;
serverAddr.sin_addr.s_addr = INADDR_ANY;
serverAddr.sin_port = htons(SERVER_PORT);
if (bind(sfd, (struct sockaddr*)&serverAddr, sizeof(struct sockaddr)) == -1)
{
perror("bind");
close(sfd);
exit(-1);
}
if(listen(sfd, LISENT_NUM) == -1)
{
perror("listen");
close(sfd);
exit(-1);
}
printf("#@ listen SERVER_PORT %d\\n", SERVER_PORT);
printf("main: server waiting connect...\\n");
if ((cfd = accept(sfd, (struct sockaddr *)&clientaddr, (socklen_t*)&size)) == -1)
{
perror("accept");
close(sfd);
return0;
}
printf("client (ip = %s : SERVER_PORT = %d) connect success\\n", inet_ntoa(clientaddr.sin_addr), ntohs(clientaddr.sin_port));
while (1)
{
usleep(1000*10);
if (send(cfd, "hello MYD/YG2L", 6, MSG_NOSIGNAL) == -1)
{
perror("send");
exit(-1);
}
printf("send: hello MYD/YG2L\\n");
usleep(1000*10);
if(recv(cfd, buff, sizeof(buff), 0) == -1)
{
perror("recv");
exit(-1);
}
printf("receive: %s\\n", buff);
}
return0;
}
lugl@lugl-virtual-machine:~/MYDG2L$ source /opt/yg2lx/environment-setup-aarch64-poky-linux
lugl@lugl-virtual-machine:~/MYDG2L$ $CC tcp_server.c -o tcp_server
In file included from /opt/yg2lx/sysroots/aarch64-poky-linux/usr/include/bits/libc-header-start.h:33,
from /opt/yg2lx/sysroots/aarch64-poky-linux/usr/include/stdio.h:27,
from tcp_server.c:1:
/opt/yg2lx/sysroots/aarch64-poky-linux/usr/include/features.h:397:4: warning: #warning _FORTIFY_SOURCE requires compiling with optimization (-O) [-Wcpp]
# warning _FORTIFY_SOURCE requires compiling with optimization (-O)
^~~~~~~
lugl@lugl-virtual-machine:~/MYDG2L$ ls
tcp_server tcp_server.c
lugl@lugl-virtual-machine:~/MYDG2L$ file tcp_server
tcp_server: ELF 64-bit LSB shared object, ARM aarch64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-aarch64.so.1, for GNU/Linux 3.14.0, BuildID[sha1]=c81069580d81fc0f4ac9bb00ecdd7ddf822e251f, with debug_info, not stripped
- 上傳開發(fā)板,修改執(zhí)行權(quán)限后運行:
root@myir-yg2lx:~# chmod 777 tcp_server
root@myir-yg2lx:~# ./tcp_server
#@ listen SERVER_PORT 3861
main: server waiting connect...
client (ip = 192.168.3.166 : SERVER_PORT = 53634) connect success
send: hello MYD/YG2L
receive: hello
send: hello MYD/YG2L
receive: hello
send: hello MYD/YG2L
receive: hello MYD
send: hello MYD/YG2L
- 打開TCP調(diào)試工具,連上服務(wù)器:
發(fā)送數(shù)據(jù)后,服務(wù)器打印出接收到的數(shù)據(jù),客戶端也接收到數(shù)據(jù)。
聲明:本文內(nèi)容及配圖由入駐作者撰寫或者入駐合作網(wǎng)站授權(quán)轉(zhuǎn)載。文章觀點僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場。文章及其配圖僅供工程師學(xué)習(xí)之用,如有內(nèi)容侵權(quán)或者其他違規(guī)問題,請聯(lián)系本站處理。
舉報投訴
-
TCP
+關(guān)注
關(guān)注
8文章
1378瀏覽量
79298
發(fā)布評論請先 登錄
相關(guān)推薦
基于瑞薩電子RZ/G2L的FET-G2LD-C核心板和OK-G2LD-C開發(fā)板評測
基于瑞薩電子RZ/G2L處理器開發(fā)的FET-G2LD
發(fā)表于 05-30 17:24
?1189次閱讀
【飛凌RZ/G2L開發(fā)板試用體驗】+01.開箱(zmj)
帶來的正是能滿足這些要求的飛凌RZ/G2L開發(fā)板評估板套件。1.RZ/V2L評估套件簡介
發(fā)表于 08-28 19:13
【米爾瑞薩RZ/G2L開發(fā)板-試用體驗】米爾-瑞薩RZG2L - 64位雙核MPU開發(fā)板開箱測評
剛收到米爾瑞薩RZ/G2L開發(fā)板打開包裝后看到的很大的一塊黑色PCB,做工精美的
發(fā)表于 05-22 21:53
【米爾瑞薩RZ/G2L開發(fā)板-試用體驗】米爾瑞薩RZ/G2L開發(fā)板開箱視頻
今天剛剛收到米爾瑞薩RZ/G2L開發(fā)板,拆開包裹后給人的感覺是驚艷,板卡設(shè)計真的很棒,來看看視頻
發(fā)表于 05-22 21:58
150套開發(fā)板免費送!還有5G手機拿?米爾RZ/G2L開發(fā)板創(chuàng)意秀
/12
05
送的是哪款開發(fā)板?
米爾MYD-YG2LX開發(fā)板,該款產(chǎn)品采用瑞薩
發(fā)表于 05-24 16:36
【米爾瑞薩RZ/G2L開發(fā)板-試用體驗】米爾瑞薩RZ/G2L開發(fā)板使用SSH登錄
收到的米爾瑞薩RZ/G2L開發(fā)板上電測試一下SSH登錄方式和其它測試!
SSH登錄
在使用
發(fā)表于 06-11 21:47
150套開發(fā)板免費送!還有5G手機拿?米爾RZ/G2L開發(fā)板創(chuàng)意秀
人間最美五月天不負韶華不負卿米爾又來送板子了不是3套,也不是4套150套米爾RZ/G2L開發(fā)板送!免費!板卡不回收!01這是什么樣的有獎活動
評論