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,说明是父进程,天上相应的代码就可以了。这个很简单的吧?!