im new to both C and C# language and i would appreciate any help/feedback on the following issue.
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#. 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#. the error encounter whenever the client.c tries to connect is "Connection refused" when gets this line in the code
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_a开发者_运维百科ddr = 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
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.
SUGGESTIONS:
Get a scenario where a specific client (e.g. your C client) can connect to a specific server (e.g. your C server).
Copy the same .exe's to the PC's where you're having problems and try again.
Make sure the different .exe's are using the same port#.
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):
http://www.wireshark.org
'Hope that helps!
精彩评论