我正在用 c# vs2012 编写客户端程序,并用 c 在 linux raspbian 上编写服务器程序。它们位于同一网络中,并且客户端可以正常连接到服务器。问题是,当我从客户端向服务器发送某些内容时,服务器没有反应。我不知道我的问题是在客户端还是在服务器端。
服务器代码:
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#define PORTNUM 4510
#define MAXRCVLEN 255
void set_fifo(int *fifo1, int *fifo2)
{
mkfifo("/tmp/fifo1", 0600); // 1 - Reading from serial
mkfifo("/tmp/fifo2", 0600); // 2 - Writing to serial
if((*fifo1 = open("/tmp/fifo1", O_RDONLY) < 0) || (*fifo2 = open("/tmp/fifo2", O_WRONLY) < 0))
{
error("ERROR opening FIFO\n");
exit(1);
}
}
int main(int argc, char *argv[])
{
char msg[] = "Hello World !\n";
char buffer[MAXRCVLEN];
int fifo1, fifo2;
set_fifo(&fifo1, &fifo2);
int len = 0;
struct sockaddr_in dest; /* socket info about the machine connecting to us */
struct sockaddr_in serv; /* socket info about our server */
int mysocket; /* socket used to listen for incoming connections */
socklen_t socksize = sizeof(struct sockaddr_in);
memset(&serv, 0, sizeof(serv)); /* zero the struct before filling the fields */
serv.sin_family = AF_INET; /* set the type of connection to TCP/IP */
serv.sin_addr.s_addr = htonl(INADDR_ANY); /* set our address to any interface */
serv.sin_port = htons(PORTNUM); /* set the server port number */
mysocket = socket(AF_INET, SOCK_STREAM, 0);
/* bind serv information to mysocket */
bind(mysocket, (struct sockaddr *)&serv, sizeof(struct sockaddr));
/* start listening, allowing a queue of up to 1 pending connection */
listen(mysocket, 1);
int consocket = accept(mysocket, (struct sockaddr *)&dest, &socksize);
while(consocket)
{
printf("Incoming connection from %s\n", inet_ntoa(dest.sin_addr));
recv(mysocket, buffer, MAXRCVLEN, 0)
buffer[MAXRCVLEN] = '\0';
printf("Message: %s", buffer);
//send(consocket, msg, strlen(msg), 0);
consocket = accept(mysocket, (struct sockaddr *)&dest, &socksize);
}
close(consocket);
close(mysocket);
return EXIT_SUCCESS;
}
客户端连接后,我正确收到“传入连接”消息。 客户端发送方法:
private void sendButton_Click(object sender, EventArgs e)
{
try
{
string theMessageToSend = "Hello!";
byte[] msg = Encoding.ASCII.GetBytes(theMessageToSend);
senderSock.Send(msg);
}
catch (Exception exc) { MessageBox.Show(exc.ToString()); }
}
try-catch 不会抛出任何东西。
以及我在客户端中使用的连接到服务的方法:
private void button1_Click(object sender, EventArgs e)
{
connectButton.Text = "Connecting...";
try
{
SocketPermission permission = new SocketPermission(
NetworkAccess.Connect, // Connection permission
TransportType.Tcp, // Defines transport types
"", // Gets the IP addresses
SocketPermission.AllPorts // All ports
);
permission.Demand();
IPAddress ipAddr = IPAddress.Parse(ipInput.Text);
int portNr = Convert.ToInt32(portTextBox.Text);
IPEndPoint ipEndPoint = new IPEndPoint(ipAddr, portNr);
// Create one Socket object to setup Tcp connection
senderSock = new Socket(
ipAddr.AddressFamily,// Specifies the addressing scheme
SocketType.Stream, // The type of socket
ProtocolType.Tcp // Specifies the protocols
);
senderSock.NoDelay = false; // Using the Nagle algorithm
// Establishes a connection to a remote host
senderSock.Connect(ipEndPoint);
tbStatus.Text = "Socket connected to " + senderSock.RemoteEndPoint.ToString();
connectButton.Enabled = false;
connectButton.Text = "Connected";
mainStart.Enabled = true;
}
catch (Exception exc)
{
MessageBox.Show(exc.ToString());
connectButton.Text = "Connect";
}
}
最佳答案
您在错误的套接字上调用recv()。在 consocket 上调用它。
此外,我不确定您是否正在发送带有空终止符的 C 样式字符串?我知道您在缓冲区末尾放置了一个 null,但仍然..
此外,recv() 返回一些您需要考虑的内容。
此外,如果您希望服务器正确处理多个客户端,您将需要关闭recv()循环并删除其中的accept()。
关于c# - vs2012 c# 与unix c server socket通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20070357/