[英]Connecting C socket client to a C# socket server
im new to both C and C# language and i would appreciate any help/feedback on the following issue. 我是C和C#语言的新手,对于以下问题,我将不胜感激。
Basically, im trying to make a socket connection in different programming languages. 基本上,我试图用不同的编程语言建立套接字连接。 the client side has been developed in C and the server side in C#.
客户端已使用C开发,服务器已使用C#开发。 Both application works well only when i tested in the same programming language, for example, the client side works well if the server is in C. same applies the server side in C#.
只有当我使用相同的编程语言进行测试时,这两个应用程序才能很好地工作,例如,如果服务器在C中,则客户端工作良好。相同的服务器在C#中也适用。 the error encounter whenever the client.c tries to connect is "Connection refused" when gets this line in the code
每当在代码中获取此行时,client.c尝试连接时遇到的错误为“连接被拒绝”
if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
error("ERROR connecting");
here is the code for both socket ends. 这是套接字两端的代码。
client.c 客户端
/* Client socket demo */
int sockfd, n;
struct sockaddr_in serv_addr;
struct hostent *server;
char buffer[256];
char hostname[1024];
//hostname[1023] = '\0';
struct hostent* h;
gethostname(hostname, sizeof(hostname));
//printf("Hostname: %s\n", hostname);
server = gethostbyname(hostname);
//printf("h_name: %s\n", h->h_name);
//portno = atoi(argv[2]);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
//server = gethostbyname(argv[1]);
if (server == NULL) {
fprintf(stderr,"ERROR, no such host\n");
exit(0);
}
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET; // only IPv4
memcpy(&serv_addr.sin_addr, server->h_addr, server->h_length); // fixed part
bcopy((char *)server->h_addr,(char *)&serv_addr.sin_addr.s_addr,server->h_length);
serv_addr.sin_port = htons(SERVERPORT); // htons((u_short)port); .sin_addr.S_un.S_addr = htonl(INADDR_ANY)
//serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
error("ERROR connecting");
printf("Please enter the message: ");
bzero(buffer,256);
fgets(buffer,255,stdin);
n = write(sockfd,buffer,strlen(buffer));
if (n < 0)
error("ERROR writing to socket");
bzero(buffer,256);
n = read(sockfd,buffer,255);
if (n < 0)
error("ERROR reading from socket");
printf("here we are %s\n",buffer);
close(sockfd);
server.cs server.cs
void ButtonStartListenClick(object sender, System.EventArgs e)
{
try
{
// Check the port value
if(textBoxPort.Text == "")
{
MessageBox.Show("Please enter a Port Number");
return;
}
string portStr = textBoxPort.Text;
int port = System.Convert.ToInt32(portStr);
// Create the listening socket...
m_mainSocket = new Socket(AddressFamily.InterNetwork,SocketType.Stream, ProtocolType.Tcp);
IPEndPoint ipLocal = new IPEndPoint (IPAddress.Any, port);
// Bind to local IP Address...
m_mainSocket.Bind( ipLocal );
// Start listening...
m_mainSocket.Listen(4);
// Create the call back for any client connections...
m_mainSocket.BeginAccept(new AsyncCallback (OnClientConnect), null);
UpdateControls(true);
}
catch(SocketException se)
{
MessageBox.Show ( se.Message );
}
}
The language does not matter. 语言并不重要。 Honest :)
诚实 :)
If you're running these from two separate PCs, the first thing to check is your firewall. 如果您是从两台不同的PC上运行它们,那么首先要检查的是防火墙。
SUGGESTIONS: 建议:
Get a scenario where a specific client (eg your C client) can connect to a specific server (eg your C server). 获得特定客户端(例如C客户端)可以连接到特定服务器(例如C服务器)的方案。
Copy the same .exe's to the PC's where you're having problems and try again. 将相同的 .exe复制到遇到问题的PC上,然后重试。
Make sure the different .exe's are using the same port#. 确保不同的.exe使用相同的端口号。
You might also be interested in getting Wireshark, so you can trace the packets going back and forth (particularly the TCP "SYN" packet the client issues to try to connect): 您可能还对获取Wireshark感兴趣,因此您可以跟踪来回的数据包(特别是客户端发出的尝试连接的TCP“ SYN”数据包):
'Hope that helps! 希望有帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.