GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 16-Feb-2004, 15:28
rogermark100 rogermark100 is offline
New Member
 
Join Date: Feb 2004
Posts: 6
rogermark100 is on a distinguished road

Ports


I was wondering if it was possible and how to open and close ports using C.

thankyou
  #2  
Old 16-Feb-2004, 15:59
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Quote:
Originally Posted by rogermark100
I was wondering if it was possible and how to open and close ports using C.

thankyou

Welcome Roger. Ports are pretty specific to your operating system and environment. What operating system are you using? (I assume this is PC hardware) Also, are you talking serail ports or parallel ports? Also, what do you want to do once they are open?

Under *nix, serial port communication is pretty straight forward and I can help you with that.

Under Windows, I may not be able to help you much there. I am pretty unfamiliar with the (*complicated* ) windows API.
  #3  
Old 16-Feb-2004, 16:31
rogermark100 rogermark100 is offline
New Member
 
Join Date: Feb 2004
Posts: 6
rogermark100 is on a distinguished road
It would be under linux as windows is <insert derogatory comment>.

Any help is much appreciated

Thanks

Roger
  #4  
Old 16-Feb-2004, 17:08
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Good. (I am also assuming serial ports...)

Ports in linux are pretty easy because of the dev filesystem. Opening a port can be done just like opening a file:
CPP / C++ / C Code:
      fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);

and closing a port is as simple as
CPP / C++ / C Code:
close(fd);

The part in between is the tricky part

This link provides some great information on posix serial ports and how to use them.

HTH,
d
  #5  
Old 18-Feb-2004, 15:18
rogermark100 rogermark100 is offline
New Member
 
Join Date: Feb 2004
Posts: 6
rogermark100 is on a distinguished road
Ah sorry I should of made myself a little more clear i meant tcp and udp

Thanks
  #6  
Old 18-Feb-2004, 15:26
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Okay. Did you find anything yet. I am currently working on a project where I am using ports. I adapted some code from UNIX, Network Programming with TCP/IP by Alan Dix. Here is the header from my library.
CPP / C++ / C Code:
//sc_comm.h
//Server client communication functions
//Written initially to support db_server & db_client, but should be able to be used
//with any tcp/ip communication protocol program
/*********************************************************************************
***  Credits:                                                                  ***
***     A major portion of the TCP/IP code was derived from some excellent     ***
***     short course notes "UNIX, Network Programming with TCP/IP" written by  ***
***     Alan Dix, copyright 1986.  This paper can be found at:                 ***
***     [url]www.hiraeth.com/alan/tutorials[/url].  The code derived herein is derived    ***
***     from his examples.  Any break or misimplementation is due to my less   ***
***     than adequate knowledge of the topic.                                  ***
*********************************************************************************/

#ifndef __SC_COMM
#define __SC_COMM

//System include files to enable tcp/ip communication
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>

//tcp_send WAIT MACROS
#define	WAIT	1
#define	NO_WAIT	0

//Server side functions
int 	tcp_listen(int port, char* ip_add);					//Open a socket and listen for incoming client connections
int 	tcp_loop(int port);									//Begin indefinite loop to process client connections
int		tcp_send(int socket, char* buffer, int wait);		//Send string buffer with optional wait

//Client side functions
int		tcp_connect(char *ip_add, int portno);				//Request client connection
#endif

I can upload this library and my implementation testing files if it would help.
 
 

Recent GIDBlogVista ?Widgets? on Windows XP by LocalTech

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 03:10.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.