當前位置:首頁 » 編程語言 » c語言udp編程
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

c語言udp編程

發布時間: 2022-02-21 05:40:34

A. 怎樣用c語言做socket網路編程

mfc只是對socket進行了一些封裝,大部分人做網路編程都是用的原始的socket,比如如下介面都可以在c下進行調用
1.socket()
2.bind()
3.connect()
4.listen()
5.accept()
6.send() 和recv()
7.sendto() 和recvfrom()
8.close() 和shutdown()
9.getpeername()
10.gethostname()
這些介面是在Winsock2.h中定義的不是在mfc中定義的,你只需要包含Winsock2.h頭文件和Ws2_32.lib庫就可以了。

B. C語言 UDP socket 簡單客戶端 編程,急

提一下,你那個地址不好用,換成了127.0.0.1,埠可以用,完全按照要求寫的,編譯沒錯誤,調試通過:
gcc server.c -o server
gcc client.c -o client
打開2個控制台:一個運行 ./server 另一個運行 ./client
server.c:
========================================
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <errno.h>

#define BUFFERSIZE 1024
typedef struct sockaddr SA;

int main(void)
{
char buf[BUFFERSIZE];
struct sockaddr_in addr_s;
struct sockaddr_in addr_c;
int sockfd;
socklen_t length;
int i;

if((sockfd = socket(AF_INET,SOCK_DGRAM,0)) == -1)
{
perror("socket fail");
return -1;
}

memset(&addr_s,0,sizeof(addr_s));
addr_s.sin_family = AF_INET;
addr_s.sin_addr.s_addr = inet_addr("127.0.0.1");
addr_s.sin_port = htons(31180);

if(bind(sockfd,(SA *)&addr_s,sizeof(addr_s)) == -1)
{
perror("bind fail");
return -1;
}

length = sizeof(addr_c);

memset(buf,'\0',sizeof(buf));
if(recvfrom(sockfd,buf,sizeof(buf),0
,(SA *)&addr_c,&length) == -1)
{
perror("recvfrom fail");
}
printf("recvfrom client:%s\n",buf);
sendto(sockfd,buf,sizeof(buf),0,(SA *)&addr_c,sizeof(addr_c));

close(sockfd);
}

====================================
client.c:
====================================
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <errno.h>

#define BUFFERSIZE 1024
typedef struct sockaddr SA;

int main(void)
{
int sockfd;
char buf[BUFFERSIZE];
struct sockaddr_in addr_s;

if((sockfd = socket(AF_INET,SOCK_DGRAM,0)) == -1)
{
perror("socket fail");
return -1;
}

memset(&addr_s,0,sizeof(addr_s));
addr_s.sin_family = AF_INET;
addr_s.sin_addr.s_addr = inet_addr("127.0.0.1");
addr_s.sin_port = htons(31180);

memset(buf,'\0',sizeof(buf));
sprintf(buf,"abcde");
if(sendto(sockfd,buf,sizeof(buf)
,0,(SA *)&addr_s,sizeof(addr_s)) < 0)
{
perror("sendto fail");
}
memset(buf,'\0',sizeof(buf));
recvfrom(sockfd,buf,sizeof(buf),0,NULL,NULL);
printf("recvfrom server:%s\n",buf);

close(sockfd);
}

C. C語言 UDP文件傳輸

void ThreadReceive(PVOID param)
{
static struct sockaddr_in server; // 綁定地址
static SOCKET ListenSocket; // 等待接收數據的socket
char buf[4096];
int len = sizeof(client);
int result;

ListenSocket = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);

if (ListenSocket == INVALID_SOCKET)
{
perror("opening stream socket");
exit(0);
}

server.sin_family = PF_INET;
server.sin_addr.s_addr = htonl( INADDR_ANY ); // 任何地址
server.sin_port = htons(LOCAL_PORT);

if (SOCKET_ERROR == bind(ListenSocket, (struct sockaddr *)&server, sizeof(server)) )
{
printf("Error: 綁定失敗\n");
exit(1);
closesocket( ListenSocket );
}
printf("socket port %d \n", ntohs(server.sin_port));

while(TRUE)
{
//接收數據
result = recvfrom(ListenSocket, buf, sizeof(buf)-1, 0, (struct sockaddr *)&client, &len);
if ( result > 0 )
{
buf[result] = 0;
inet_ntoa(client.sin_addr), ntohs(client.sin_port));
}
}
closesocket(ListenSocket);
}

D. Linux下的c語言UDP編程

read是直接讀取文件,scanf系列函數本身也得用read讀取文件。

bind是把socket和地址關聯,INADDR_ANY的意思是任何地址。比如你的設備有很多ip,客戶端向其中任何一個發送數據你的socket都能接收到。相應地如果你bind一個確定的ip的話,向其他ip發送數據你的socket是接收不到的。

E. 用c語言編程來計算基於udp的被動時延測量(gcc編譯)其中如何判別μtp協議,求大神指點

前面右轉C語言吧

F. 實現UDP協議傳輸的C語言程序。如被採納,可追加懸賞,盼高手指教,不勝感激!!!

原型:
int WINAPI icePub_UdpSendAndReceive(char *sendBuffer,int bufferLen,char *strIP,int port,char *receiveBuffer,int timeoutSeconds,int retryCounts)
輸入:sendBuffer 發送的數據
bufferLen sendBuffer的長度
strIP 服務端地址
port 埠
timeoutSeconds 超時時間,秒
retryCounts 接收失敗重發次數
輸出:receiveBuffer 接收的數據
返回碼:接收數據的長度

char buff[1024],buff2[1024*10];
int receiveLen;

strcpy(buff,"tag:01\r\ncommand:reboot\r\ndata:none\r\n");

typedef int (WINAPI ICEPUB_UDPSENDANDRECEIVE)(char *sendBuffer,int bufferLen,char *strIP,int port,char *receiveBuffer,int timeoutSeconds,int retryCounts);
ICEPUB_UDPSENDANDRECEIVE *icePub_UdpSendAndReceive = 0;
HINSTANCE hDLLDrv = LoadLibrary("icePubDll.dll");
if(hDLLDrv)
icePub_UdpSendAndReceive=(ICEPUB_UDPSENDANDRECEIVE *)GetProcAddress(hDLLDrv,"icePub_UdpSendAndReceive");
if(icePub_UdpSendAndReceive)
receiveLen=icePub_UdpSendAndReceive(buff,strlen(buff),"192.168.1.111",6000,buff2,15,1);
if(hDLLDrv)
FreeLibrary(hDLLDrv);

AfxMessageBox(buff2);

G. C語言的UDP socket怎麼在處理一個請求的同時接受另一個請求呢

建線程,同步接收

H. 有沒有windows下c語言實現udp協議的代碼

Windows下C語言的Socket編程例子(TCP和UDP)
一。<TCP>
server端:
復制代碼
1#include"stdafx.h"
2#include<stdio.h>
3#include<winsock2.h>
4
5#pragmacomment(lib,"ws2_32.lib")
6
7intmain(intargc,char*argv[])
8{
9//初始化WSA
10WORDsockVersion=MAKEWORD(2,2);
11WSADATAwsaData;
12if(WSAStartup(sockVersion,&wsaData)!=0)
13{
14return0;
15}
16
17//創建套接字
18SOCKETslisten=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
19if(slisten==INVALID_SOCKET)
20{
21printf("socketerror!");
22return0;
23}
24
25//綁定IP和埠
26sockaddr_insin;
27sin.sin_family=AF_INET;
28sin.sin_port=htons(8888);
29sin.sin_addr.S_un.S_addr=INADDR_ANY;
30if(bind(slisten,(LPSOCKADDR)&sin,sizeof(sin))==SOCKET_ERROR)
31{
32printf("binderror!");
33}
34
35//開始監聽
36if(listen(slisten,5)==SOCKET_ERROR)
37{
38printf("listenerror!");
39return0;
40}
41
42//循環接收數據
43SOCKETsClient;
44sockaddr_inremoteAddr;
45intnAddrlen=sizeof(remoteAddr);
46charrevData[255];
47while(true)
48{
49printf("等待連接... ");
50sClient=accept(slisten,(SOCKADDR*)&remoteAddr,&nAddrlen);
51if(sClient==INVALID_SOCKET)
52{
53printf("accepterror!");
54continue;
55}
56printf("接受到一個連接:%s ",inet_ntoa(remoteAddr.sin_addr));
57
58//接收數據
59intret=recv(sClient,revData,255,0);
60if(ret>0)
61{
62revData[ret]=0x00;
63printf(revData);
64}
65
66//發送數據
67char*sendData="你好,TCP客戶端! ";
68send(sClient,sendData,strlen(sendData),0);
69closesocket(sClient);
70}
71
72closesocket(slisten);
73WSACleanup();
74return0;
75}
復制代碼
client端:
復制代碼
1#include"stdafx.h"
2#include<WINSOCK2.H>
3#include<STDIO.H>
4
5#pragmacomment(lib,"ws2_32.lib")
6
7
8intmain(intargc,char*argv[])
9{
10WORDsockVersion=MAKEWORD(2,2);
11WSADATAdata;
12if(WSAStartup(sockVersion,&data)!=0)
13{
14return0;
15}
16
17SOCKETsclient=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
18if(sclient==INVALID_SOCKET)
19{
20printf("invalidsocket!");
21return0;
22}
23
24sockaddr_inserAddr;
25serAddr.sin_family=AF_INET;
26serAddr.sin_port=htons(8888);
27serAddr.sin_addr.S_un.S_addr=inet_addr("127.0.0.1");
28if(connect(sclient,(sockaddr*)&serAddr,sizeof(serAddr))==SOCKET_ERROR)
29{
30printf("connecterror!");
31closesocket(sclient);
32return0;
33}
34char*sendData="你好,TCP服務端,我是客戶端! ";
35send(sclient,sendData,strlen(sendData),0);
36
37charrecData[255];
38intret=recv(sclient,recData,255,0);
39if(ret>0)
40{
41recData[ret]=0x00;
42printf(recData);
43}
44closesocket(sclient);
45WSACleanup();
46return0;
47}
復制代碼

二.<UDP>
SERVER端
復制代碼
1#include"stdafx.h"
2#include<stdio.h>
3#include<winsock2.h>
4
5#pragmacomment(lib,"ws2_32.lib")
6
7intmain(intargc,char*argv[])
8{
9WSADATAwsaData;
10WORDsockVersion=MAKEWORD(2,2);
11if(WSAStartup(sockVersion,&wsaData)!=0)
12{
13return0;
14}
15
16SOCKETserSocket=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);
17if(serSocket==INVALID_SOCKET)
18{
19printf("socketerror!");
20return0;
21}
22
23sockaddr_inserAddr;
24serAddr.sin_family=AF_INET;
25serAddr.sin_port=htons(8888);
26serAddr.sin_addr.S_un.S_addr=INADDR_ANY;
27if(bind(serSocket,(sockaddr*)&serAddr,sizeof(serAddr))==SOCKET_ERROR)
28{
29printf("binderror!");
30closesocket(serSocket);
31return0;
32}
33
34sockaddr_inremoteAddr;
35intnAddrLen=sizeof(remoteAddr);
36while(true)
37{
38charrecvData[255];
39intret=recvfrom(serSocket,recvData,255,0,(sockaddr*)&remoteAddr,&nAddrLen);
40if(ret>0)
41{
42recvData[ret]=0x00;
43printf("接受到一個連接:%s ",inet_ntoa(remoteAddr.sin_addr));
44printf(recvData);
45}
46
47char*sendData="一個來自服務端的UDP數據包 ";
48sendto(serSocket,sendData,strlen(sendData),0,(sockaddr*)&remoteAddr,nAddrLen);
49
50}
51closesocket(serSocket);
52WSACleanup();
53return0;
54}
復制代碼
CLIENT端
復制代碼
1#include"stdafx.h"
2#include<stdio.h>
3#include<winsock2.h>
4
5#pragmacomment(lib,"ws2_32.lib")
6
7intmain(intargc,char*argv[])
8{
9WORDsocketVersion=MAKEWORD(2,2);
10WSADATAwsaData;
11if(WSAStartup(socketVersion,&wsaData)!=0)
12{
13return0;
14}
15SOCKETsclient=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);
16
17sockaddr_insin;
18sin.sin_family=AF_INET;
19sin.sin_port=htons(8888);
20sin.sin_addr.S_un.S_addr=inet_addr("127.0.0.1");
21intlen=sizeof(sin);
22
23char*sendData="來自客戶端的數據包. ";
24sendto(sclient,sendData,strlen(sendData),0,(sockaddr*)&sin,len);
25
26charrecvData[255];
27intret=recvfrom(sclient,recvData,255,0,(sockaddr*)&sin,&len);
28if(ret>0)
29{
30recvData[ret]=0x00;
31printf(recvData);
32}
33
34closesocket(sclient);
35WSACleanup();
36return0;
37}

I. linux c語言實現,udp協議

UDP協議全稱是用戶數據報協議,在網路中它與TCP協議一樣用於處理數據包,是一種無連接的協議。在OSI模型中,在第四層--傳輸層,處於IP協議的上一層。UDP有不提供數據包分組、組裝和不能對數據包進行排序的缺點,也就是說,當報文發送之後,是無法得知其是否安全完整到達的。UDP用來支持那些需要在計算機之間傳輸數據的網路應用。包括網路視頻會議系統在內的眾多的客戶/伺服器模式的網路應用都需要使用UDP協議。UDP協議從問世至今已經被使用了很多年,雖然其最初的光彩已經被一些類似協議所掩蓋,但是即使是在今天UDP仍然不失為一項非常實用和可行的網路傳輸層協議。

J. C語言 網路編程 udp問題

這個可以實現呀!fork出一個子進程,給舉起返回值,如果返回值為0,說明是子進程,然後用一個while(1)死循環,不斷的調用recvfrom();如果返回值不為0,說明是父進程,天上相應的代碼就可以了。這個很簡單的吧?!