- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一台服务器和一台客户端。服务器和客户端有 2 个 NIC。这是在 Fedora 26 上开发的。
我正在通过我的“数据通道”10.0.0.210:21323 -> 10.0.0.209:21323 从客户端发送一个包含数据包编号 (qualitySequenceCounter) 和 40 字节编码音频文件 (.chn) 的缓冲区。当服务器收到数据时,它会解包 qualitySequenceCounter 和音频数据。然后服务器应该通过“控制 channel ”10.0.0.121:8080 -> 10.0.0.122:8080 将 qualitySequenceCounter 发送回客户端。服务器通过“控制 channel ”发送数据,但客户端从未收到它,只是卡在那里等待。我不知道我错过了什么或做错了什么。
服务器
// server2s.cpp
// parsing files
#include <iostream>
#include <fstream>
#include <string>
#include <sys/stat.h>
//network stuff
#include <sys/socket.h>
#include <netdb.h>
#include <memory.h>
#include <stdio.h>
#include <stdarg.h>
//#define SOL_IP IPPROTO_IP
#ifndef INVALID_SOCKET
#define INVALID_SOCKET -1
#endif
#define sockerrno errno
char szLineBuf[500];
int iNetSock = INVALID_SOCKET;
int iRequest = 1;
struct sockaddr_in sAddr, cAddr;
socklen_t iAddrLen = sizeof(sAddr);
socklen_t cAddrLen = sizeof(cAddr);
int perr(const char *pszFormat, ...)
{
va_list argList;
va_start(argList, pszFormat);
vsnprintf(szLineBuf, sizeof(szLineBuf)-10, pszFormat, argList);
szLineBuf[sizeof(szLineBuf)-10] = '\0';
printf("Error: %s\n", szLineBuf);
return 0;
}
//++++++++++++++++++++++++++++++++++++++
// second socket for control channel
char szLineBufCC[500];
int iNetSockCC = INVALID_SOCKET;
int iRequestCC = 1;
struct sockaddr_in sAddrCC, cAddrCC;
socklen_t iAddrLenCC = sizeof(sAddrCC);
socklen_t cAddrLenCC = sizeof(cAddrCC);
int perrCC(const char *pszFormat, ...)
{
va_list argList;
va_start(argList, pszFormat);
vsnprintf(szLineBufCC, sizeof(szLineBufCC)-10, pszFormat, argList);
szLineBufCC[sizeof(szLineBufCC)-10] = '\0';
printf("Error CC: %s\n", szLineBufCC);
return 0;
}
// end
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
int main(int argc, const char * argv[]) {
//file I/O stuff
// this is the char array that we will populate with the control channel counter and audio data
char buffer[44];
// buffer used only for audio data to be written to file
char audioBuffer[40];
// this is the char array for the control channel counter
char bufferCC[4];
// this is for opening the audio file
std::ifstream inFile;
std::ofstream outFile;
// initializing the location of the audio channel read
long fileLoc = 0;
// initializing the control channel counter
int qualitySequenceCounter=0;
// initializing how we will be able to tell where we are in the audio file; need to add code to loop back to beginning of the audio file once we reach the end
struct stat results;
//network stuff
const char *pszHost = "10.0.0.209";
unsigned short iPort = 21323;
//+++++++++++++++++++++++++++++++++++++++
// second socket for control channel
//network stuff
const char *pszHostCC = "10.0.0.121";
unsigned short iPortCC = 8080;
// end
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//open up audio file for parsing
outFile.open("/home/mode1_received.chn", std::ios::out | std::ios::binary);
//if the file doesn't open, tell me
if(!outFile.is_open()){
std::cout << "Error opening audio file\n";
}
if (stat("/home/mode1_received.chn", &results) == 0) {
// The size of the file in bytes is in
// results.st_size
std::cout << "File size:" << results.st_size << "\n";
}
else {
std::cout << "An error occured getting the file length";
// An error occurred
}
//establish the IP component
memset((char *)&sAddr, 0,sizeof(sAddr));
memset((char *)&cAddr, 0,sizeof(cAddr));
sAddr.sin_family = AF_INET;
sAddr.sin_port = htons(iPort);
struct hostent *pHost = gethostbyname(pszHost);
memcpy(&sAddr.sin_addr.s_addr, pHost->h_addr, pHost->h_length);
//creates the UDP socket to send data to
if ((iNetSock = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
return 9+perr("cannot create socket");
// Bind the socket with the server address
if ( bind(iNetSock, (const struct sockaddr *)&sAddr,
sizeof(sAddr)) < 0 )
{
perror("bind failed");
exit(EXIT_FAILURE);
}
//++++++++++++++++++++++++++++++++++++++
// second socket for CC
//establish the IP component
memset((char *)&sAddrCC, 0,sizeof(sAddrCC));
memset((char *)&cAddrCC, 0,sizeof(cAddrCC));
sAddrCC.sin_family = AF_INET;
sAddrCC.sin_port = htons(iPortCC);
struct hostent *pHostCC = gethostbyname(pszHostCC);
memcpy(&sAddrCC.sin_addr.s_addr, pHostCC->h_addr, pHostCC->h_length);
//creates the UDP socket to send data to
if ((iNetSockCC = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
return 9+perrCC("cannot create socket for CC: Control Channel");
// Bind the socket with the server address
if ( bind(iNetSockCC, (const struct sockaddr *)&sAddrCC,
sizeof(sAddrCC)) < 0 )
{
perror("bind failed for CC");
exit(EXIT_FAILURE);
}
// End
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// divide size of audio file by 40
while(qualitySequenceCounter < 103574){
// Get data
unsigned int n;
socklen_t len = sizeof(cAddr);
//char *pData = &buffer[0];
n = recvfrom(iNetSock, (char *)buffer, 44,
MSG_WAITALL, ( struct sockaddr *) &cAddr,
&len);
printf("data received\n");
memcpy(&qualitySequenceCounter, buffer, sizeof(int));
memcpy(&audioBuffer, &buffer[4], 40);
printf("qualitySequenceCounter : %u\n", qualitySequenceCounter);
// this copies our counter that we are using for the feedback loop into the 4 byte char array
memcpy(&bufferCC[0], (char *)&(qualitySequenceCounter), sizeof(int));
// this sets up the payload for the UDP transmission
//char *pDataCC = &bufferCC[0];
// sending the data via UDP
sendto(iNetSockCC, bufferCC, 4, 0, (struct sockaddr *)&cAddrCC, cAddrLenCC);
printf("data sent back\n");
// this code allows us to check the counter by printing it to std out
/*int counterCheck = *((int *)buffer);
std::cout << "Counter check: " << counterCheck << "\n";
const char* beg = buffer;
const char* end = beg + sizeof(buffer);
while(beg != end)
std::cout << std::bitset<8>(*beg++) << ' ';
std::cout << '\n';
*/
//printf("qualitySequenceCounter : %u\n", qualitySequenceCounter);
//if (buffer == NULL)
// break;
outFile << audioBuffer;
//buffer[n] = '\0';
//printf("Client : %s\n", pData);
//printf("Client : %u\n", n);
// qualcounter is 103574
// will need to reset the fileLoc counter to 0 once we reach the end of the audio file to loop the demo
}
//close output file
outFile.close();
return 0;
}
客户端
// client2s.cpp
// parsing files
#include <iostream>
#include <fstream>
#include <string>
#include <sys/stat.h>
//network stuff
#include <sys/socket.h>
#include <netdb.h>
#include <memory.h>
#include <stdio.h>
#include <stdarg.h>
//#define SOL_IP IPPROTO_IP
#ifndef INVALID_SOCKET
#define INVALID_SOCKET -1
#endif
#define sockerrno errno
char szLineBuf[500];
int iNetSock = INVALID_SOCKET;
int iRequest = 1;
struct sockaddr_in sAddr;
socklen_t iAddrLen = sizeof(sAddr);
int perr(const char *pszFormat, ...)
{
va_list argList;
va_start(argList, pszFormat);
vsnprintf(szLineBuf, sizeof(szLineBuf)-10, pszFormat, argList);
szLineBuf[sizeof(szLineBuf)-10] = '\0';
printf("Error: %s\n", szLineBuf);
return 0;
}
//++++++++++++++++++++++++++++++++++++++++
// second socket for control channel
char szLineBufCC[500];
int iNetSockCC = INVALID_SOCKET;
int iRequestCC = 1;
struct sockaddr_in sAddrCC;
socklen_t iAddrLenCC = sizeof(sAddrCC);
int perrCC(const char *pszFormat, ...)
{
va_list argList;
va_start(argList, pszFormat);
vsnprintf(szLineBufCC, sizeof(szLineBufCC)-10, pszFormat, argList);
szLineBufCC[sizeof(szLineBufCC)-10] = '\0';
printf("Error with CC: %s\n", szLineBufCC);
return 0;
}
// End
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
int main(int argc, const char * argv[]) {
//file I/O stuff
// this is the char array that we will populate with the control channel counter and audio data
char buffer[44];
// this is the char array for the control channel counter
char bufferCC[4];
// this is for opening the audio file
std::ifstream inFile;
// initializing the location of the audio channel read
long fileLoc = 0;
// initializing the control channel counter
int qualitySequenceCounter=0;
// counter received over control channel CC
int qualityCC=0;
// initializing how we will be able to tell where we are in the audio file; need to add code to loop back to beginning of the audio file once we reach the end
struct stat results;
//network stuff
const char *pszHost = "10.0.0.209";
unsigned short iPort = 21323;
//++++++++++++++++++++++++++++++++++++++++
// second socket for control channel
//network stuff
const char *pszHostCC = "10.0.0.121";
unsigned short iPortCC = 8080;
// End
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//open up audio file for parsing
inFile.open("/home/mode1.chn", std::ios::in | std::ios::binary);
//if the file doesn't open, tell me
if(!inFile.is_open()){
std::cout << "Error opening audio file\n";
}
if (stat("/home/mode1.chn", &results) == 0)
// The size of the file in bytes is in
// results.st_size
std::cout << "File size:" << results.st_size << "\n";
else
std::cout << "An error occured getting the file length";
// An error occurred}
//establish the IP component
memset((char *)&sAddr, 0,sizeof(sAddr));
sAddr.sin_family = AF_INET;
sAddr.sin_port = htons(iPort);
struct hostent *pHost = gethostbyname(pszHost);
memcpy(&sAddr.sin_addr.s_addr, pHost->h_addr, pHost->h_length);
//creates the UDP socket to send data to
if ((iNetSock = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
return 9+perr("cannot create socket");
//++++++++++++++++++++++++++++++++++++
// second socket for control channel
//establish the IP component
memset((char *)&sAddrCC, 0,sizeof(sAddrCC));
sAddrCC.sin_family = AF_INET;
sAddrCC.sin_port = htons(iPortCC);
struct hostent *pHostCC = gethostbyname(pszHostCC);
memcpy(&sAddrCC.sin_addr.s_addr, pHostCC->h_addr, pHostCC->h_length);
//creates the UDP socket to send data to
if ((iNetSockCC = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
return 9+perrCC("cannot create socket for Control Channel");
// End
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
while(qualitySequenceCounter < results.st_size && fileLoc >= 0){
// will need to open file inside the while loop once we add the control channel feedback, else we need to open all of the different files at once (which might be easier) and switch back and forth between multiple open files
// this reads in 40 bytes of data and places it 4 bytes into the char array
inFile.read(&buffer[4],40);
// getting the location of where we are in the audio file in case we need to switch audio files
fileLoc = inFile.tellg();
// this copies our counter that we are using for the feedback loop into the first 4 bytes of the char array
memcpy(&buffer[0], (char *)&(qualitySequenceCounter), sizeof(int));
// this sets up the payload for the UDP transmission
char *pData = &buffer[0];
// sending the data via UDP
sendto(iNetSock, pData, 44, 0, (struct sockaddr *)&sAddr, iAddrLen);
printf("data sent\n");
//++++++++++++++++++++++++++++++++++++
// second socket for control channel
// Get data
unsigned int nCC;
socklen_t lenCC = sizeof(sAddrCC);
//char *pData = &buffer[0];
nCC = recvfrom(iNetSockCC, (char *)bufferCC, 4,
MSG_WAITALL, ( struct sockaddr *) &sAddrCC,
&lenCC);
printf("data received again\n");
memcpy(&qualityCC, bufferCC, sizeof(int));
printf("qualityCC : %u\n", qualityCC);
// End
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// increment the feedback counter for the control channel
qualitySequenceCounter++;
// this code allows us to check the counter by printing it to std out
/* int counterCheck = *((int *)buffer);
std::cout << "Counter check: " << counterCheck << "\n";
const char* beg = buffer;
const char* end = beg + sizeof(buffer);
while(beg != end)
std::cout << std::bitset<8>(*beg++) << ' ';
std::cout << '\n';
*/
// will need to reset the fileLoc counter to 0 once we reach the end of the audio file to loop the demo
}
//close input file
inFile.close();
return 0;
}
最佳答案
客户端从不将 iNetSockCC
绑定(bind)到任何特定端口,因此它不会监听任何特定端口。那么它如何才能收到服务器的响应呢?我有点不明白你为什么要两对 socket 。为什么不让服务器通过发回源来回复客户端?
关于c++ - UDP 客户端/服务器程序,通过不同 IP 和端口上的 2 个套接字进行通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57333677/
我正在维护一些 Java 代码,我目前正在将它们转换为 C#。 Java 代码是这样做的: sendString(somedata + '\000'); 在 C# 中,我正在尝试做同样的事情: sen
如何确定函数中传递的参数是字符串还是字符(不确定如何正确调用它)文字? 我的函数(不正确): void check(const char* str) { // some code here }
我真的不知道如何准确地提出这个问题,但我希望标题已经说明了这一点。 我正在寻找一种方法(一个框架/库),它提供了执行 String.contains() 函数的能力,该函数告诉我给定的字符串是否与搜索
我正在尝试编写一些读取 Lambda 表达式并输出 beta 缩减版本的东西。 Lambda 的类型如下:\variable -> expression,应用程序的形式为 (表达式) (表达式)。因此
StackOverflow 上的第 1 篇文章,如果我没能把它做好,我深表歉意。我陷入了一个愚蠢的练习,我需要制作一个“刽子手游戏”,我尝试从“.txt”文件中读取单词,然后我得到了我的加密函数,它将
我想在 Groovy 中测试我的 Java 自定义注释,但由于字符问题而未能成功。 Groovyc: Expected 'a' to be an inline constant of type cha
当我尝试在单击按钮期间运行 javascript location.href 时,出现以下错误“字 rune 字中的字符过多”。 最佳答案 这应该使用 OnClientClick相反? 您可能还想停
我想要类似的东西: let a = ["v".utf8[0], 1, 2] 我想到的最接近的是: let a = [0x76, 1, 2] 和 "v".data(using: String.Encod
有没有办法在 MySQL 中指定 Unicode 字 rune 字? 我想用 Ascii 字符替换 Unicode 字符,如下所示: Update MyTbl Set MyFld = Replace(
阅读 PNG 规范后,我有点惊讶。我读过字 rune 字应该用像 0x41 这样的二进制值进行硬编码,而不是在(程序员友好的)'A' 中。问题似乎是在具有不同底层字符集的不同系统上编译期间字 rune
考虑一个具有 UTF-8 执行字符集的 C++11 编译器(并且符合要求 char 类型为有符号 8 位字节的 x86-64 ABI) . 字母 Ä(元音变音)具有 0xC4 的 unicode 代码
为什么即使有 UTF-8 字符串文字,C11 或 C++11 中也没有 UTF-8 字 rune 字?我知道,一般来说,字 rune 字表示单个 ASCII 字符,它与单字节 UTF-8 代码点相同,
我怎样才能用 Jade 做到这一点? how would I do this 我几乎可以做任何事情,除了引入一个 span 中间句子。 最佳答案 h3.blur. how would I do t
这似乎是一个非常简单的问题,但我只是想澄清我的疑问。我正在查看其他开发人员编写的代码。有一些涉及 float 的计算。 示例:Float fNotAvlbl = new Float(-99); 他为什
我想知道第 3 行“if dec:”中的“dec”是什么意思 1 def dec2bin(dec): 2 result='' 3 if dec:
我试图在字符串中查找不包含任何“a”字符的单词。我写了下面的代码,但它不起作用。我怎么能对正则表达式说“不包括”?我不能用“^”符号表示“不是”吗? import re string2 = "asfd
这个问题在这里已经有了答案: Is floating point math broken? (31 个答案) Is floating point arbitrary precision availa
我正在创建一个时尚的文本应用程序,但在某些地方出现错误(“字 rune 字中的字符太多”)。我只写了一个字母,但是当我粘贴它时,它会转换成许多这样的字母:“\uD83C\uDD89”,原始字母是“🆉
我正在尝试检查用户是否在文本框中输入了一个数字值,是否接受了小数位。非常感谢任何帮助。 Private Sub textbox1_AfterUpdate() If IsNumeric(textbox1
我知道一个 Byte 是 8 位,但其他的代表什么?我正在参加一个使用摩托罗拉 68k 架构的汇编类(class),我对目前的词汇感到困惑。 最佳答案 如 operator's manual for
我是一名优秀的程序员,十分优秀!