473,508 Members | 2,128 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

linux socket help

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

Expand|Select|Wrap|Line Numbers
  1.  
  2. /*
  3. ** server.c -- a stream socket server demo
  4. */
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <unistd.h>
  9. #include <errno.h>
  10. #include <string.h>
  11. #include <sys/types.h>
  12. #include <sys/socket.h>
  13. #include <netinet/in.h>
  14. #include <arpa/inet.h>
  15. #include <sys/wait.h>
  16. #include <signal.h>
  17. #include <string.h>
  18.  
  19. #define MYPORT 3490
  20.  
  21. #define BACKLOG 10
  22.  
  23. #define MAXDATASIZE 100
  24.  
  25. void sigchld_handler(int s)
  26. {
  27. while(wait(NULL) > 0);
  28. }
  29.  
  30. int main(void)
  31. {
  32. int sockfd, new_fd;
  33. struct sockaddr_in my_addr;
  34. struct sockaddr_in their_addr;
  35. int sin_size;
  36. struct sigaction sa;
  37. int yes=1;
  38. char msg[200]={""};
  39. int msgLen;
  40. int numbytes;
  41. char buf[MAXDATASIZE];
  42.  
  43.  
  44.  
  45. if ((sockfd = socket(AF_INET, SOCK_STREAM, 0))== -1) {
  46. perror("socket");
  47. exit(1);
  48. }
  49.  
  50. if
  51. (setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int))== -1) {
  52. perror("setsockopt");
  53. exit(1);
  54. }
  55.  
  56. my_addr.sin_family = AF_INET;
  57. my_addr.sin_port = htons(MYPORT);
  58. my_addr.sin_addr.s_addr = INADDR_ANY;
  59. memset(&(my_addr.sin_zero), '\0', 8);
  60.  
  61. if (bind(sockfd, (struct sockaddr *)&my_addr,sizeof(struct
  62. sockaddr)== -1)
  63. {
  64. perror("bind");
  65. exit(1);
  66. }
  67.  
  68. if (listen(sockfd, BACKLOG) == -1) {
  69. perror("listen");
  70. exit(1);
  71. }
  72.  
  73. sa.sa_handler = sigchld_handler;
  74. sigemptyset(&sa.sa_mask);
  75. sa.sa_flags = SA_RESTART;
  76. if (sigaction(SIGCHLD, &sa, NULL) == -1) {
  77. perror("sigaction");
  78. exit(1);
  79. }
  80. sin_size = sizeof(struct sockaddr_in);
  81. if ((new_fd = accept(sockfd, (structsockaddr *)&their_addr,
  82. &sin_size)) == -1)
  83. {
  84. perror("accept");
  85. }
  86. printf("server: got connection
  87. from%s\n",inet_ntoa(their_addr.sin_addr));
  88. while(1)
  89. {
  90. //    This code works great sending a message to my other VB
  91. app
  92. //*********************************************
  93. //printf("Enter a message to sent: ");
  94. //gets(msg);
  95. //msgLen=strlen(msg);
  96. //   if (send(new_fd, msg, msgLen, 0) ==-1)
  97. //       perror("send");
  98. //********************************************
  99.  
  100. //this is the code that doesnt work
  101. //*********************************************************************
  102.  
  103. if ((numbytes=recv(sockfd, buf, MAXDATASIZE-1, 0)) ==-1)
  104. {
  105. perror("recv");
  106. exit(1);
  107. }
  108.  
  109. buf[numbytes] = '\0';
  110.  
  111. printf("Received: %s\n",buf);
  112.  
  113. }
  114. //************************************************************************
  115. return 0;
  116. }
  117.  
  118.  
  119.  

Nov 14 '05 #1
3 5985
Robert Smith wrote:

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.

/*
** server.c -- a stream socket server demo
*/

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
Not a standard include file
#include <errno.h>
#include <string.h>
#include <sys/types.h>
Not a standard include file
#include <sys/socket.h>
Not a standard include file
#include <netinet/in.h>
Not a standard include file
#include <arpa/inet.h>
Not a standard include file
#include <sys/wait.h>
Not a standard include file
#include <signal.h>
#include <string.h>


With all those errors we cannot possibly understand your
non-portable code here. Try a newsgroup that deals with your
actual system.

--
"I'm a war president. I make decisions here in the Oval Office
in foreign policy matters with war on my mind." - Bush.
"If I knew then what I know today, I would still have invaded
Iraq. It was the right decision" - G.W. Bush, 2004-08-02
Nov 14 '05 #2
Ash
> if (bind(sockfd, (struct sockaddr *)&my_addr,sizeof(struct
sockaddr)== -1)
{
perror("bind");
exit(1);
}


Here the bind arguments are not proper. The third argument should
contain the size of the address structure whichis sizeof(my_addr).
Nov 14 '05 #3
On Sun, 05 Sep 2004 16:12:03 GMT, "Robert Smith" <no**@none.com>
wrote:
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 :)
Sockets, and (other) Linux specifics, are offtopic in clc, which deals
with standard/portable C. Try comp.unix.programming or (I think it is)
comp.os.linux.development.apps. But:
<snip> if ((new_fd = accept(sockfd, (structsockaddr *)&their_addr,
&sin_size)) == -1) <snip> // if (send(new_fd, msg, msgLen, 0) ==-1) <snip> if ((numbytes=recv(sockfd, buf, MAXDATASIZE-1, 0)) ==-1)


Think about which fd is which.

- David.Thompson1 at worldnet.att.net
Nov 14 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
4969
by: Tom Jones | last post by:
I need to access databases on both win2k and Linux systems but I have to set up the DSNs under program control ... no uses using GUIs ... Has anyone done this? if so can we discuss it either in...
0
3620
by: Google Mike | last post by:
After a lot of thought and research, and playing with FreeTDS and InlineTDS, as well as various ODBC connections, I have determined that the fastest and cheapest way to get up and going with PHP on...
7
17930
by: gurtd_dauberie | last post by:
I can't get the correct IP address on Linux... The following code works properly on Windows and Unix. But on Linux, I always get the Loopback Address (127.0.0.1) Here is the code : //gets the...
2
2569
by: xunil | last post by:
hi, could any one please help show me how to run a python script in linux ? when i try "python script.py" it just does nothing this is the script iam trying to run #!/usr/bin/env python...
9
20877
by: 8anos | last post by:
Hello, I am new at the community and newbie at programming :) As you may know rapidshare provides a perl script for linux, to upload files at their servers. You can find the original scripts at...
5
2782
by: ganeshp | last post by:
Hi , In Java using socket programming is it possible to have a server program on windows that services a client program on linux? I tried the code in the below given link:...
1
4264
by: getafixx | last post by:
Hello everyone, We have a linux server (Fedora core 7, default install, firewall turned off) and a bunch of windows XP machines on network/domain. All machines are visible and I can get to both...
3
11635
by: TsanChung | last post by:
I want to make a java TCP socket client to communicate with a TCP server socket on linux. Are there some sample C unix server and java client socket programs available? The Richard Stevens'...
4
5485
by: The Doctor | last post by:
Hey people, I have two applications: the server, which creates a server socket, waits for a real-time signal, and if it receives one, it creates a client socket. The client, which will...
0
7326
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7383
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7046
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5627
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4707
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3194
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3182
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1557
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
418
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.