Monday, December 27, 2010

【 Weak current College 】 workstation and PC real-time communication between process 】


Summary of the article briefly describes SUN workstation and PC to communicate between the commonly used methods, highlighted an approach based on Socket programming, inter-process communication methods directly in real time, and gives examples.
With the growing popularity of the computer and the ever-expanding field of application, one on your computer's performance requirements are also getting higher and higher, particularly in scientific computing, graphics and image processing, network engineering, computer aided design and manufacturing (CAD/CAM), and software engineering and other fields. These areas require for General micro-computers, it is difficult to do, and what happens to the workstation appears to meet these needs. SUN workstation is a typical representative of the workstation, because it uses the RISC technology and a series of advanced technologies and methods, making its price-performance far beyond the traditional micro-, small computer system, and thus have a very high market share. On the other hand, the IBM PC and compatible in China is also quite common, which in all walks of life have a wide range of applications, has a large number of users and hardware products.
How will SUN workstation and PC that organically, enabling them to their long, becomes a very real problem. For example, in the distributed application system, since the PC has a large number like switch signal and analog i/o interface board like this, so it can be used as industrial control field in the lower computer, complete the on-site data real-time acquisition and related control operations; and SUN workstation as a PC, for general data processing and analysis. In such a distributed application systems, SUN workstation and PC communication is a critical issue.
SUN workstation and PC communication can be divided into two areas: one is the hardware connections, second, the software programming. For hardware connections, in General can have the following two approaches, the first is to use SUN workstation itself RS-423 serial RS-232/RS-232 serial port with a PC, for serial asynchronous communication. The biggest advantage is the low cost, the disadvantage is that the data transmission speed is slow, data error checking and correction work completed by the users themselves, also on the hardware direct programming. The second method is to use the SUN workstation itself is equipped with an Ethernet interface, then the requirements for the PC with a network interface card, the two are connected by a coaxial cable, constitutes Ethernet, thereby achieving their communication. The advantage of this approach is the data transfer speed, is ideal for real-time request high application projects, coupled with system software and programming tools package of support, so the programming work is also quite simple. The disadvantage of this method is more costly than the first method slightly higher.
This article will highlight it in the second hardware connection mode, that is, on an Ethernet network environment, in order to achieve SUN workstation and PC communication between processes, the software needs to do some work.
We know that SUN workstation uses a known as NFS (Network File System) distributed file system, the biggest advantage is independent of the models, operating systems and network architecture that enables heterogeneous environment of file sharing. In NFS system support, file access transparent to the user, that user's use of the network files and use local files that you want.
In order to achieve the PC and SUN workstations to share files between, we must also be installed on a PC is called PC-NFS software, the software provided by SUN company. Installed the software, the PC user can use the local hard drive on the soft, so use files on SUN workstations.
Here, thanks to the SUN NFS and PC-NFS both systems to interact with the software, you can complete the SUN workstation and PC communication between processes, with the means of communication is through file sharing. But this approach in practice often seem speed less than ideal, but because of the need to rely on file for the media, so the timing and the operation of the additional overhead. So, we need to explore a SUN workstation and PC between processes through file sharing, but directly for real-time communication. The author through a period of exploration and practice, to find a feasible solution, are made for your reference.
This implementation of direct communication method between processes is essentially in interconnection network domain (Internet Domain) to support the TCP/IP Protocol, the Socket-based (socket) system call program design. Socket is on the network at the transport layer provides an interface to the application, its objective is to achieve the network communication between processes. Here we pass a specific instance of the detailed description of this method.
Due to Socket programming typically use Client/Server (client/server) model, so we also follow the practical realization of this principle, the SUN workstation as a Server, PC as Client, both in program design Division. In this case, we want to accomplish is the process on the workstation and on your PC's processes for bi-directional data transfer, the program respectively to a string to represent the actual information you want to transfer.
Due to Socket programming is a more complex issue, this article does not detail, the content of the reader is referred to the appropriate books (such as SUN's Network Programming, etc.). We are here just to program several issues to members with a brief description.
1. as a result of the processes on the SUN workstation belongs to the Server side, so it must run first, wait for the process from PCClient party connection requests;
2. the processes running on the workstation, the first on a screen a port (Port) number. Port number is the TCP/IP protocol to identify the process as an integral part of the address, the workstation process port number together with the workstation's machine name (HostNAme) must be sent to the PC on which you want to communicate with the process, only in this way, your PC's processes, and they knew that they will be and which processes occur. In this instance used by the command line parameters passed to the information, of course you can also use other methods;
3. in the above example program that processes data transmission using a read () and write () function, but these two functions are used for the Socket used for the file at the time of the actual operation that is not exactly the same. Because the network itself limits the transmission characteristics, they are likely in a call cannot read or write function parameters required so much the quantity of data. Therefore in order to complete the file operation similar to read () and write () function is the same function, we specifically prepared two new function readpkt () and writepkt (), a few times into or write the number of bytes required;
4. implementation of the program on the workstation as long as the workstation can be compiled and put into operation when connected, a program running on your PC must have a SUN company provides PC-NFS Programer's Toolkit package of support, the package provides Socket calls for all functions. This PC program is used on Borland c++ 3.0 compiler compiled, in addition, the package also supports the Microsoft C language and the Microsoft Windows application programming. In the generated executable code, that is a .exe file, it will no longer depend on this package, but still want to run under the system support PC-NFS.
Above is the SUN workstation and PC for real-time communication between processes. Of course, this "real-time" is relative, because the SunOS operating system belonging to the UNIX operating system this class, and the UNIX operating system itself is a timesharing operating system, there can be no absolute real-time. But this article discuss this method in real-world projects that have been tested to meet the requirements of real-time performance, as SUN workstation and PC high-speed data transfers opened a new road.
Finally it should be noted that this article describes the process of real-time communication between not only apply to SUN workstation and PC, but also to connect to the Ethernet network on all machines, including SUN workstations and SUN workstation process of communication between the PC and the process of communication between the PC and any combination among them. SUN workstation (or Server side) program: the program file name: SUN.C
SUN implementation method:
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h><BR>#include <netdb.h>
#include <stdio.h>
#define DATA "1234567890"
main()
{
int sock,length;
struct sockaddr-in server;
int msgsock;
char buf;
int readpkt(),writepkt();
sock=socket(AF-INET,SOCK-STREAM,0);
IF (SOCK<0) {
perror("opening stream socket");
exit(1);
}
server.sin-family=AF-INET;
server.sin-addr.s-addr=INADDR-ANY;
server.sin-port=0;
if (bind(sock,(struct sockaddr *)&server,sizeof server)<0){
perror("binding stream socket");
exit(1);
}
length=sizeof server;
if (getsockname(sock,(struct sockaddr *)&server,&length)<0){
perror("getting socket name");
exit(1);
}
printf("Socket port #%d",ntohs(server.sin-port));
listen(sock,5);
msgsock=accept(sock,(struct sockaddr *)0,(int *)0);
if (msgsock==-1){
perror("accept");
exit(1);
}
else do{
bzero(buf,sizeof buf);
if (readpkt(msgsock,buf,1024)<0){
perror("reading stream message");
break;
}prinft("%s",buf);
strcpy(buf,DATA);
if (writepkt(msgsock,buf,1024)<0) {
perror("writing stream message");
break;
}
}while (1);
close(msgsock);
close(sock);
return 0;
}
int readpkt(sock,buf,size)
int sock;
char *buf;
int size;
{
int rest, readnum,count;
count=0;
rest=size-count;
while (rest) {
readnum=read(sock,buf,rest);
if (readnum<0) return readnum;
rest-=readnum;
buf+=readnum;
}
return (size-rest);
}
int writepkt(sock,buf,size)
int sock;
char *buf;
int size;
{
int rest,writenum,count;
count=0;
rest=size-count;
while (rest){
writenumwrite(sock,buf,rest);
if (writenum<0) return writenum;
rest-=writenum;
buf+=writenum;
}
return (size-rest);
}
PC (Client side) program:
Program name: PC.C
Implementation method: PC <SUN工作站名称><SUN进程端口号>
#include <sys/tk-types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#define DATA "abcdefghijklmnopqrstuvwxyz"
main(argc,argv)
int argc;
char *argv;
{
int sock;
struct sockaddr-in server;
struct hostent *hp,*gethostbyname();
char buf;
int readpkt(),writepkt();
sock=socket(PF-INET,SOCK-STREAM,0);
if (sock<0){
perror("opening stream socket");
exit(1);
}
server.sin-family=AF-INET;
hp=gethostbyname(argv);
if (hp==0){
fprintf(stderr,"%s:unknown host.",argv);
exit(2);
}
memcpy((char *)&server.sin-addr,(char *)hp->h-addr,hp->h-length);
server.sin-port=htons(atoi(argv));
if (connect(sock,(struct sockaddr *)&server,sizeof server)<0){
perror("connecting stream socket");
exit(1);
}.
do {
strcpy(buf,DATA);
if (writepkt(sock,buf,1024)<0){
perror("writing on stream socket");
break;
}
memset(buf,0,1024);
if (readpkt(sock,buf,1024)<0){
perror("reading from stream socket");
break;
}
printf("%s",buf);
} while(1);
close(sock);
return 0;
}
int readpkt(sock,buf,size)
int sock;
char *buf;
int size;
{
int rest,readnum,count;
count=0;
rest=size-count;
while (rest) {
readnum=read(sock,buf,rest);
if (readnum<0) return readnum;
rest-=readnum;
buf+=readnum;
}
return(size-rest);
}
int writepkt(sock,buf,size)
int sock;
char *buf;
int size;
{
int rest,writenum,count;
count=0;
rest=size-count;
while (rest) {
writenum=write(sock,buf,rest);
if (writenum<0) return writenum;
rest-=writenum;
buf+=writenum;
}
return (size-rest);

No comments:

Post a Comment