R
Robert Smith
As you can tell by my code that I will post I am obviously new with linux
socket programming so to be to hard on me
When I run my little program I get this error:
Server: got connection from 192.168.0.5
recv: Transport endpoint is not connected.
I don't know what that means or why I am getting it.
I know the connection is made by the server: message.
My other end program is a VB program. When i uncomment out the appropriate
lines in the following code my VB app works just find for receiving
messages.
Thanks
socket programming so to be to hard on me
When I run my little program I get this error:
Server: got connection from 192.168.0.5
recv: Transport endpoint is not connected.
I don't know what that means or why I am getting it.
I know the connection is made by the server: message.
My other end program is a VB program. When i uncomment out the appropriate
lines in the following code my VB app works just find for receiving
messages.
Thanks
Code:
/*
** server.c -- a stream socket server demo
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <signal.h>
#include <string.h>
#define MYPORT 3490
#define BACKLOG 10
#define MAXDATASIZE 100
void sigchld_handler(int s)
{
while(wait(NULL) > 0);
}
int main(void)
{
int sockfd, new_fd;
struct sockaddr_in my_addr;
struct sockaddr_in their_addr;
int sin_size;
struct sigaction sa;
int yes=1;
char msg[200]={""};
int msgLen;
int numbytes;
char buf[MAXDATASIZE];
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0))== -1) {
perror("socket");
exit(1);
}
if
(setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int))== -1) {
perror("setsockopt");
exit(1);
}
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(MYPORT);
my_addr.sin_addr.s_addr = INADDR_ANY;
memset(&(my_addr.sin_zero), '\0', 8);
if (bind(sockfd, (struct sockaddr *)&my_addr,sizeof(struct
sockaddr)== -1)
{
perror("bind");
exit(1);
}
if (listen(sockfd, BACKLOG) == -1) {
perror("listen");
exit(1);
}
sa.sa_handler = sigchld_handler;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART;
if (sigaction(SIGCHLD, &sa, NULL) == -1) {
perror("sigaction");
exit(1);
}
sin_size = sizeof(struct sockaddr_in);
if ((new_fd = accept(sockfd, (structsockaddr *)&their_addr,
&sin_size)) == -1)
{
perror("accept");
}
printf("server: got connection
from%s\n",inet_ntoa(their_addr.sin_addr));
while(1)
{
// This code works great sending a message to my other VB
app
//*********************************************
//printf("Enter a message to sent: ");
//gets(msg);
//msgLen=strlen(msg);
// if (send(new_fd, msg, msgLen, 0) ==-1)
// perror("send");
//********************************************
//this is the code that doesnt work
//*********************************************************************
if ((numbytes=recv(sockfd, buf, MAXDATASIZE-1, 0)) ==-1)
{
perror("recv");
exit(1);
}
buf[numbytes] = '\0';
printf("Received: %s\n",buf);
}
//************************************************************************
return 0;
}