以下两段代码编译通过,但我在客户端收到了 connect() failed 错误。 (使用 MinGW 编译)。
客户端代码:
// thanks to cs.baylor.edu/~donahoo/practical/CSockets/code/TCPEchoClientWS.c
#include <stdio.h>
#include <winsock.h>
#include <stdlib.h>
#define RCVBUFSIZE 32 // size of receive buffer
void DieWithError(char *errorMessage);
int main(int argc, char* argv[])
{
int sock;
struct sockaddr_in echoServAddr;
unsigned short echoServPort;
char *servIP;
char *echoString;
char echoBuffer[RCVBUFSIZE];
int echoStringLen;
int bytesRcvd, totalBytesRcvd;
WSAData wsaData;
if((argc < 3) || (argc > 4)){
fprintf(stderr, "Usage: %s <Sever IP> <Echo Word> [<Echo Port>]\n", argv[0]);
exit(1);
}
if (argc==4)
echoServPort = atoi(argv[3]); // use given port if any
else
echoServPort = 7; // echo is well-known port for echo service
if(WSAStartup(MAKEWORD(2, 0), &wsaData) != 0){ // load winsock 2.0 dll
fprintf(stderr, "WSAStartup() failed");
exit(1);
}
// create reliable, stream socket using tcp
if((sock=socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
DieWithError("socket() failed");
// construct the server address structure
memset(&echoServAddr, 0, sizeof(echoServAddr));
echoServAddr.sin_family = AF_INET;
echoServAddr.sin_addr.s_addr = inet_addr(servIP); // server IP address
echoServAddr.sin_port = htons(echoServPort);
// establish connection to the echo server
if(connect(sock, (struct sockaddr*)&echoServAddr, sizeof(echoServAddr)) < 0)
DieWithError("connect() failed");
echoStringLen = strlen(echoString); // determine input length
// send the string, includeing the null terminator to the server
if(send(sock, echoString, echoStringLen, 0)!= echoStringLen)
DieWithError("send() sent a different number of bytes than expected");
totalBytesRcvd = 0;
printf("Received: "); // setup to print the echoed string
while(totalBytesRcvd < echoStringLen){
// receive up to the buffer size (minus 1 to leave space for a null terminator) bytes from the sender
if(bytesRcvd = recv(sock, echoBuffer, RCVBUFSIZE-1, 0) <= 0)
DieWithError("recv() failed or connection closed prematurely");
totalBytesRcvd += bytesRcvd; // keep tally of total bytes
echoBuffer[bytesRcvd] = '\0';
printf("%s", echoBuffer); // print the echo buffer
}
printf("\n");
closesocket(sock);
WSACleanup();
exit(0);
}
void DieWithError(char *errorMessage)
{
fprintf(stderr, "%s: %d\n", errorMessage, WSAGetLastError());
exit(1);
}
服务器代码:
// thanks cs.baylor.edu/~donahoo/practical/CSockets/code/TCPEchoServerWS.c
#include <stdio.h>
#include <winsock.h>
#include <stdlib.h>
#define MAXPENDING 5 // maximum outstanding connection requests
#define RCVBUFSIZE 1000
void DieWithError(char *errorMessage);
void HandleTCPClient(int clntSocket); // tcp client handling function
int main(int argc, char **argv)
{
int serverSock;
int clientSock;
struct sockaddr_in echoServerAddr;
struct sockaddr_in echoClientAddr;
unsigned short echoServerPort;
int clientLen; // length of client address data structure
WSAData wsaData;
if (argc!=2){
fprintf(stderr, "Usage: %s <Server Port>\n", argv[0]);
exit(1);
}
echoServerPort = atoi(argv[1]);
if(WSAStartup(MAKEWORD(2, 0), &wsaData)!=0){
fprintf(stderr, "WSAStartup() failed");
exit(1);
}
// create socket for incoming connections
if((serverSock=socket(PF_INET, SOCK_STREAM, IPPROTO_TCP))<0)
DieWithError("socket() failed");
// construct local address structure
memset(&echoServerAddr, 0, sizeof(echoServerAddr));
echoServerAddr.sin_family = AF_INET;
echoServerAddr.sin_addr.s_addr = htonl(INADDR_ANY); // any incoming interface
echoServerAddr.sin_port = htons(echoServerPort); // local port
// bind to the local address
if(bind(serverSock,
(struct sockaddr*)&echoServerAddr,
sizeof(echoServerAddr)
)<0)
DieWithError("bind() failed");
// mark the socket so it will listen for incoming connections
if(listen(serverSock, MAXPENDING)<0)
DieWithError("listen() failed");
for (;;){ // run forever
// set the size of the in-out parameter
clientLen = sizeof(echoClientAddr);
// wait for a client to connect
if((clientSock = accept(serverSock, (struct sockaddr*)&echoClientAddr,
&clientLen)) < 0)
DieWithError("accept() failed");
// clientSock is connected to a client
printf("Handling client %s\n", inet_ntoa(echoClientAddr.sin_addr));
HandleTCPClient(clientSock);
}
// NOT REACHED
}
void DieWithError(char *errorMessage)
{
fprintf(stderr, "%s: %d\n", errorMessage, WSAGetLastError());
exit(1);
}
void HandleTCPClient(int clientSocket)
{
char echoBuffer[RCVBUFSIZE]; // buffer for echostring
int recvMsgSize; // size of received message
// receive message from client
if((recvMsgSize = recv(clientSocket, echoBuffer, RCVBUFSIZE, 0) <0))
DieWithError("recv() failed");
// send received string and receive again until end of transmission
while(recvMsgSize > 0){
// echo message back to client
if(send(clientSocket, echoBuffer, recvMsgSize, 0)!=recvMsgSize)
DieWithError("send() failed");
// see if there's more data to receive
if((recvMsgSize = recv(clientSocket, echoBuffer, RCVBUFSIZE, 0)) <0)
DieWithError("recv() failed");
}
closesocket(clientSocket); // close client socket
}
我该如何解决这个问题?
最佳答案
您没有初始化 servIP:
servIP = argv[1];
我认为您真的需要熟悉您的调试器,因为您应该能够使用调试器在几秒钟内发现这种错误。
关于c Windows connect() 失败。错误 10049,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2854858/
大约一年前,我决定确保每个包含非唯一文本的Flash通知都将从模块中的方法中获取文本。我这样做的最初原因是为了避免一遍又一遍地输入相同的字符串。如果我想更改措辞,我可以在一个地方轻松完成,而且一遍又一遍地重复同一件事而出现拼写错误的可能性也会降低。我最终得到的是这样的:moduleMessagesdefformat_error_messages(errors)errors.map{|attribute,message|"Error:#{attribute.to_s.titleize}#{message}."}enddeferror_message_could_not_find(obje
我遵循MichaelHartl的“RubyonRails教程:学习Web开发”,并创建了检查用户名和电子邮件长度有效性的测试(名称最多50个字符,电子邮件最多255个字符)。test/helpers/application_helper_test.rb的内容是:require'test_helper'classApplicationHelperTest在运行bundleexecraketest时,所有测试都通过了,但我看到以下消息在最后被标记为错误:ERROR["test_full_title_helper",ApplicationHelperTest,1.820016791]test
我已经构建了一些serverspec代码来在多个主机上运行一组测试。问题是当任何测试失败时,测试会在当前主机停止。即使测试失败,我也希望它继续在所有主机上运行。Rakefile:namespace:specdotask:all=>hosts.map{|h|'spec:'+h.split('.')[0]}hosts.eachdo|host|begindesc"Runserverspecto#{host}"RSpec::Core::RakeTask.new(host)do|t|ENV['TARGET_HOST']=hostt.pattern="spec/cfengine3/*_spec.r
我是rails的新手,想在form字段上应用验证。myviewsnew.html.erb.....模拟.rbclassSimulation{:in=>1..25,:message=>'Therowmustbebetween1and25'}end模拟Controller.rbclassSimulationsController我想检查模型类中row字段的整数范围,如果不在范围内则返回错误信息。我可以检查上面代码的范围,但无法返回错误消息提前致谢 最佳答案 关键是您使用的是模型表单,一种显示ActiveRecord模型实例属性的表单。c
我正在尝试编写一个将文件上传到AWS并公开该文件的Ruby脚本。我做了以下事情:s3=Aws::S3::Resource.new(credentials:Aws::Credentials.new(KEY,SECRET),region:'us-west-2')obj=s3.bucket('stg-db').object('key')obj.upload_file(filename)这似乎工作正常,除了该文件不是公开可用的,而且我无法获得它的公共(public)URL。但是当我登录到S3时,我可以正常查看我的文件。为了使其公开可用,我将最后一行更改为obj.upload_file(file
我克隆了一个rails仓库,我现在正尝试捆绑安装背景:OSXElCapitanruby2.2.3p173(2015-08-18修订版51636)[x86_64-darwin15]rails-v在您的Gemfile中列出的或native可用的任何gem源中找不到gem'pg(>=0)ruby'。运行bundleinstall以安装缺少的gem。bundleinstallFetchinggemmetadatafromhttps://rubygems.org/............Fetchingversionmetadatafromhttps://rubygems.org/...Fe
在Cooper的书BeginningRuby中,第166页有一个我无法重现的示例。classSongincludeComparableattr_accessor:lengthdef(other)@lengthother.lengthenddefinitialize(song_name,length)@song_name=song_name@length=lengthendenda=Song.new('Rockaroundtheclock',143)b=Song.new('BohemianRhapsody',544)c=Song.new('MinuteWaltz',60)a.betwee
我是Google云的新手,我正在尝试对其进行首次部署。我的第一个部署是RubyonRails项目。我基本上是在关注thisguideinthegoogleclouddocumentation.唯一的区别是我使用的是我自己的项目,而不是他们提供的“helloworld”项目。这是我的app.yaml文件runtime:customvm:trueentrypoint:bundleexecrackup-p8080-Eproductionconfig.ruresources:cpu:0.5memory_gb:1.3disk_size_gb:10当我转到我的项目目录并运行gcloudprevie
我有两个Rails模型,即Invoice和Invoice_details。一个Invoice_details属于Invoice,一个Invoice有多个Invoice_details。我无法使用accepts_nested_attributes_forinInvoice通过Invoice模型保存Invoice_details。我收到以下错误:(0.2ms)BEGIN(0.2ms)ROLLBACKCompleted422UnprocessableEntityin25ms(ActiveRecord:4.0ms)ActiveRecord::RecordInvalid(Validationfa
这个问题在这里已经有了答案:Arraysmisbehaving(1个回答)关闭6年前。是否应该这样,即我误解了,还是错误?a=Array.new(3,Array.new(3))a[1].fill('g')=>[["g","g","g"],["g","g","g"],["g","g","g"]]它不应该导致:=>[[nil,nil,nil],["g","g","g"],[nil,nil,nil]]