Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using <socket.h>

Status
Not open for further replies.

dialsforsriram

Technical User
Jul 25, 2000
2
0
0
US
Visit site
Hi,<br>I wrote these two programs for client/server commn' using Unix. (Refer Design of Unix OS by, MauriceBach chapter 11).<br><br>The server code follows :<br><br>#include &lt;sys/socket.h&gt;<br>#include &lt;sys/types.h&gt;<br>void main()<br>{<br> int sd,ns;<br> char buf[256];<br> struct sockaddr sockaddr;<br> int fromlen;<br> sd=socket(AF_UNIX,SOCK_STREAM,0);<br> if(sd == -1)<br> {<br> printf(&quot;error&quot;);<br> }<br> bind(sd,&quot;sockname&quot;,sizeof(&quot;sockname&quot;)-1);<br> listen(sd,1);<br> for(;;)<br> {<br> ns=accept(sd,&sockaddr,&fromlen);<br> if(fork()==0)<br> {<br> close(sd);<br> read(ns,buf,sizeof(buf));<br> printf(&quot;SERVER READ %s \n&quot;,buf);<br> exit();<br> }<br> close(ns);<br> }<br>}<br><br>The Client side Code is this !<br><br>#include &lt;sys/types.h&gt;<br>#include &lt;sys/socket.h&gt;<br>void main()<br>{<br> int sd,ns;<br> char buf[256];<br> struct sockaddr sockaddr;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sd=socket(AF_UNIX,SOCK_STREAM,0);<br> if(connect(sd,&quot;sockname&quot;,sizeof(&quot;sockname&quot;)-1) == -1)<br> exit();<br> write(sd,&quot;hello world&quot;,11);<br>}<br><br>But when i execute 'em(from same user id using 2-telnet connections) I'm not gettin' any response from both sides!<br>The server just waits on and on..and client gets executed and shell prompt appears..!<br>I'm using HP UNIX !<br><br>waiting for ur replies Guys !<br>Narasimhan Sriram
 
I am not really experienced in Unix-Domain Sockets, but my impression is that in your server code there is missing a Port (Service) definition.<br>This port must be set in a special socket structure if you are using inet sockets.<br><br>I have posted in the C++ Forum a Client program for Internet sockets with Windows. Perhaps you may have a look there. The programming basics are the same in Unix and Windows.<br><br>The main thread there was Cantwell (if I remember correct)<br><br>Good Luck <p>hnd<br><a href=mailto:hasso55@yahoo.com>hasso55@yahoo.com</a><br><a href= > </a><br>
 
Sorry I was wrong before,<br><br>The mainthread was <b>Trainlogan</b>.<br><br> <p>hnd<br><a href=mailto:hasso55@yahoo.com>hasso55@yahoo.com</a><br><a href= > </a><br>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top