How to write a Measurement Telnet Server

Similar documents
How to write a Measurement Telnet Server

CS 3516: Computer Networks

CS321: Computer Networks Socket Programming

ECE 435 Network Engineering Lecture 2

CS321: Computer Networks Introduction to Application Layer

Network Programming Worksheet 2. Simple TCP Clients and Servers on *nix with C.

TCP Echo Application: Client & Server. TA: Awad A Younis Class: CS457 Fall 2014

ECE 435 Network Engineering Lecture 2

Lab 0. Yvan Petillot. Networks - Lab 0 1

Hyo-bong Son Computer Systems Laboratory Sungkyunkwan University

// socket for establishing connections

Application Programming Interfaces

Sockets. Dong-kun Shin Embedded Software Laboratory Sungkyunkwan University Embedded Software Lab.

NETWORK PROGRAMMING. Instructor: Junaid Tariq, Lecturer, Department of Computer Science

CS4514 B08 HELP Session 1

Simple network applications using sockets (BSD and WinSock) Revision 1 Copyright Clifford Slocombe

Sockets. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

CSE 333 SECTION 8. Sockets, Network Programming

CSE 333 SECTION 6. Networking and sockets

Piotr Mielecki Ph. D.

TCP/IP Sockets in C: Practical Guide for Programmers. Computer Chat. Internet Protocol (IP) IP Address. Transport Protocols. Ports

Lecture 7. Followup. Review. Communication Interface. Socket Communication. Client-Server Model. Socket Programming January 28, 2005

TCP Network Programming in C

Elementary TCP Sockets

Programming with TCP/IP. Ram Dantu

Introduction to Client-Server Model

CSE 333 SECTION 7. C++ Virtual Functions and Client-Side Network Programming

CS 640: Computer Networking

Network Socket Programming - 3 BUPT/QMUL

Network Socket Programming - 3 BUPT/QMUL

CSC209H Lecture 9. Dan Zingaro. March 11, 2015

Unix Network Programming

CSE 333 Lecture 16 - network programming intro

Network Programming in C: The Berkeley Sockets API. Networked Systems 3 Laboratory Sessions

PA #2 Reviews. set_name, get_name, del_name. Questions? Will be modified after PA #4 ~

EECS122 Communications Networks Socket Programming. Jörn Altmann

UNIX Network Programming. Overview of Socket API Network Programming Basics

CS4700/CS5700 Fundamentals of Computer Networking

file://j:\macmillancomputerpublishing\chapters\ir451.html 3/22/01

System Programming. Sockets

CS307 Operating Systems Processes

Processes. Process Concept. The Process. The Process (Cont.) Process Control Block (PCB) Process State

Signal Example 1. Signal Example 2

Network Programming in C. Networked Systems 3 Laboratory Sessions and Problem Sets

ECE322 Systems Programming Project 2: Networking with Matrix Multiplication in C Grant Kimes 12/16/15

1 /* client.c - adapted from code for example client program that uses TCP */ 2 /*Modified by Vincent Chu, Winter

Tutorial on Socket Programming

CSE 333 SECTION 7. Client-Side Network Programming

F28HS2 Hardware-Software Interface. Lecture 3 - Programming in C 3

The Berkeley Sockets API. Networked Systems Architecture 3 Lecture 4

CSE 333 SECTION 3. POSIX I/O Functions

Azblink API for Sending XMPP Messages via HTTP POST

ICT 6544 Distributed Systems Lecture 5

Socket Programming TCP UDP

Cracking WEP Keys Using WEPCrack

Shared Memory Memory mapped files

PRINCIPLES OF OPERATING SYSTEMS

Assignment 1: tcpbridge

Sockets. Dong-kun Shin Embedded Software Laboratory Sungkyunkwan University Embedded Software Lab.

MSc Integrated Electronics Networks Assignment. Investigation of TCP/IP Sockets and Ports. Gavin Cameron

Message passing systems are popular because they support client-server interactions, where: clients send messages to servers requesting a server.

CSCI 4210 Operating Systems CSCI 6140 Computer Operating Systems Sample Exam 2 Questions (document version 1.0)

Linux Network Programming, Part 1

NETWORK AND SYSTEM PROGRAMMING

Client-server model The course that gives CMU its Zip! Network programming Nov 27, Using ports to identify services.

POSIX Shared Memory. Linux/UNIX IPC Programming. Outline. Michael Kerrisk, man7.org c 2017 November 2017

Oral. Total. Dated Sign (2) (5) (3) (2)

UNIX System Programming

CSE 333 Lecture network programming intro

Socket Programming for TCP and UDP

Socket programming in C

CSE 333 Section 3. Thursday 12 April Thursday, April 12, 12

System Programming. Sockets: examples

Introduction to Socket Programming

CSC 271 Software I: Utilities and Internals

How do we Communicate? Introduction to Unix Network Programming. What does Alice do? What does Bob do? Two simplest networking programs

518 Lecture Notes Week 12

Network Programming November 3, 2008

ECE 650 Systems Programming & Engineering. Spring 2018

Client/Server. Networking Approach.

Network Programming Week #1. K.C. Kim

Linux TCP Bind Shell from Scratch with Intel x86 Assembly

chat.h #ifndef _CHAT_H #define _CHAT_H #define LBUFFSIZE 128 #define CBUFFSIZE 331

TCP/IP Sockets in C: Practical Guide for Programmers. Computer Chat. Internet Protocol (IP) IP Address. Transport Protocols

Computer Network Lab, SS Fachgebiet Technische Informatik, Joachim Zumbrägel. Overview. Sockets. Sockets in C.

ECE264 Fall 2013 Exam 3, November 20, 2013

Lecture 24. Thursday, November 19 CS 375 UNIX System Programming - Lecture 24 1

Sockets 15H2. Inshik Song

Tutorial 1 C Tutorial: Pointers, Strings, Exec

WinSock. What Is Sockets What Is Windows Sockets What Are Its Benefits Architecture of Windows Sockets Network Application Mechanics

CompSci 356: Computer Network Architectures Lecture 3: Hardware and physical links References: Chap 1.4, 1.5 of [PD] Xiaowei Yang

Lab Exam 1 D [1 mark] Give an example of a sample input which would make the function

Client software design

CS4514 (C04) HELP Session 1 Introduction to Network Programming (v1.3)

CSCI-243 Exam 2 Review February 22, 2015 Presented by the RIT Computer Science Community

Lab Exam 1 D [1 mark] Give an example of a sample input which would make the function

CSE 333 SECTION 3. POSIX I/O Functions

Socket Programming. CSIS0234A Computer and Communication Networks. Socket Programming in C

Good Luck! Marking Guide. APRIL 2014 Final Exam CSC 209H5S

Transcription:

How to write a Measurement Telnet Server A measurement Telnet server allows you to access remote I/Os with a standard Telnet client program. The following samples shows a way to set the LEDs of a DNP/EVA2 evaluation board with a Telnet client program. The C sample code in this document forms a measurement (MSR) Telnet server for the DNP/1110. The server allows you to enter values between 0 and 255 within a Telnet client window. This value is converted to a 8-bit binary number. The measurement Telnet server writes the binary number to the DNP/1110 parallel I/O port A. This port drives the DNP/EVA2 LED array. 1. Step: Download the sample source code msrtsvr6.c from www.dilnetpc.com (i.e. enter the URL www.dilnetpc.com/msrtsvr6.c to your browser). The following listing shows this sample code. Please note: We have remove some text within some printf-statements for cutting the total length of some lines. // msrtsvr6.c: Simple MSR Telnet server program. Use cross-gcc // Vers. 2.00-24.July 2002 // k.d.walter@t-online.de // Includes #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <fcntl.h> #include <sys/mman.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> // Typedefs typedef unsigned char u8; typedef unsigned short u16; typedef unsigned int u32; // Defines SSV EMBEDDED SYSTEMS 2002, mht-sa09.doc, Rev. 1.01. 1

#define GPLR (*(pgpio_base + 0x00 / 4)) #define GPDR (*(pgpio_base + 0x04 / 4)) #define GPSR (*(pgpio_base + 0x08 / 4)) #define GPCR (*(pgpio_base + 0x0C / 4)) #define MAP_PAGESIZE 4096UL // SA-1110: GPLR @ 0x90040000 // SA-1110: GPDR @ 0x90040004 // SA-1110: GPSR @ 0x90040008 // SA-1110: GPCR @ 0x9004000C // For pmapmemory //====================================================================== // Functions: 1. pmapmemory void *pmapmemory (off_t phy_addr, size_t phy_lenght) { int ifd; void *pmem; if ((phy_addr % MAP_PAGESIZE)!= 0) { fprintf (stderr,"\n Physical address "); return (NULL); if ((phy_lenght % MAP_PAGESIZE)!= 0) { fprintf (stderr,"\n Physical lenght "); return (NULL); // Open mem device for read/write... ifd= open("/dev/mem", O_RDWR O_SYNC); if (ifd < 0) { fprintf (stderr,"\n Open of /dev/mem fail!\n"); return (NULL); // Get pointer to DNP1110 memory... pmem= mmap (NULL,phy_lenght,(PROT_READ PROT_WRITE), MAP_SHARED,iFd,phy_addr); if ((pmem == MAP_FAILED) (pmem == NULL)) { fprintf (stderr,"\n Mmap of /dev/mem fail!\n"); return (NULL); // Close mem device... if (close(ifd)!= 0) fprintf(stderr,"\n Close of /dev/mem fail!\n"); SSV EMBEDDED SYSTEMS 2002, mht-sa09.doc, Rev. 1.01. 2

return (pmem); //====================================================================== // Functions: 2. main int main (int argc, char* argv[]) { int nlen= sizeof (struct sockaddr), nret, s1, s2, noline, link, cnt, i, err; char szbuf[256], szline[256]; struct sockaddr_in saserver, saclient; volatile u32 *pgpio_base; // Check user access rights... if (geteuid ()!= 0) { printf ("\n No root access rights!\n"); // Map SA-1110 GPIO register space at 0x9004000 / size 4096 bytes... pgpio_base= pmapmemory (0x90040000, 0x00001000); if (pgpio_base == NULL) { printf("\n Can not map memory at 0x90040000!\n"); // Check for port argument. Print usage if no port number... if (argc!= 2) { printf ("\n Usage.: msrtsvr6 PortNumber"); printf ("\n Sample: msrtsvr6 5000<enter>\n"); // Set DNP/1110 PIO port A = output. Reset port to 0... GPDR = 0xFF00; GPCR = (0xff << 8); GPSR = (0 << 8); // GPDR = 0xXXXXFFXX // GPCR = 0x0000XX00 // GPSR = 0x0000XX00 // Create a TCP/IP stream socket... s1= socket (AF_INET, SOCK_STREAM, 0); //... and check for error... SSV EMBEDDED SYSTEMS 2002, mht-sa09.doc, Rev. 1.01. 3

if (s1 < 0) { printf ("\n Can not create socket.\n"); // Fill in the server internet address structure... saserver.sin_family= AF_INET; // Address family saserver.sin_addr.s_addr= INADDR_ANY; // Any IP address saserver.sin_port= htons (atoi (argv[1])); // Portnumber memset (&(saserver.sin_zero), 0x00, 8); // Zero the rest... // Bind IP address/portnumber to socket... nret= bind (s1, (struct sockaddr *) &saserver, sizeof (struct sockaddr)); //... and check for any errors... if (nret < 0) { printf ("\n Can not bind socket to address structure.\n"); close (s1); // Execute a listen (queue client connect requests)... nret= listen (s1, 5); //... and check for any errors... if (nret < 0) { printf ("\n Can not use listen function.\n"); close (s1); // Server loop. Wait for client request without end... while (1) { // Waiting for connect from client... printf ("\n Waiting for connect..."); fflush (stdout); if ((s2= accept (s1, (struct sockaddr *) &saclient, &nlen)) < 0) { printf ("\n Can not use accept function.\n"); SSV EMBEDDED SYSTEMS 2002, mht-sa09.doc, Rev. 1.01. 4

close (s1); close (s2); printf ("\n Connect to %s\n", inet_ntoa (saclient.sin_addr)); fflush (stdout); // Send usage message to client... sprintf (szbuf, "\033[2J\033[H"); // Clear screen, cursor home sprintf (szbuf, "MSR-Telnet-Server. Vers. 1.00 "); sprintf (szbuf, "Enter number 0-255 and press enter or "); // We have a link... link= 1; while (link) { // Get next line with number or CTRL-C... noline= 1; err= 0; cnt= 0; sprintf (szbuf, ">"); memset (szline, 0, sizeof (szbuf)); while (noline) { // Wait for client data... memset (szbuf, 0, sizeof (szbuf)); nret= recv (s2, szbuf, sizeof (szbuf), 0); // Watch the link state... if (nret <= 0) { link= 0; noline=0; else { // Diconnect by client // Remove all char s from szbuf... i= 0; while (nret!= 0 && noline!= 0 && link!= 0) { SSV EMBEDDED SYSTEMS 2002, mht-sa09.doc, Rev. 1.01. 5

// If valid char: save data... if (szbuf[i] >= 0x30 && szbuf[i] <= 0x39) { szline[cnt]= szbuf[i]; cnt++; else { // Count invalid char s... if (szbuf[i]!= '\r' && szbuf[i]!= 0x04) err++; // Check for line end ('\r') or max char count... if (szbuf[i] == '\r' cnt > 16) noline= 0; // Check for session end (CTRL-D)... if (szbuf[i] == 0x04) { link= 0; noline=0; // Next char... i++; nret--; // We have a new line... if (link == 1) { // Check for valid number (0-255)... if (strlen (szline) == 0 atoi (szline) > 255 err!= 0) sprintf (szbuf, "ERR\n\r"); else { sprintf (szbuf, "OK\n\r"); // Output operation: write value to port A SSV EMBEDDED SYSTEMS 2002, mht-sa09.doc, Rev. 1.01. 6

GPCR = ((atoi (szline)^0xff) << 8); GPSR = (atoi (szline) << 8); // GPCR = 0x0000XX00 // GPSR = 0x0000XX00 // Close data socket... close (s2); 2. Step: Build a executable form the source code. Use the following listing for a makefile. With a makefile simply enter make msrtsvr6 for build a executable. CROSS = /usr/local/arm/2.95.2/bin/arm-linux- CC = $(CROSS)gcc CFLAGS = -Wall -O2 -march=armv4 -mtune=strongarm PROJ = telnet all: $(PROJ).o $(CC) $(CFLAGS) -o $(PROJ) $(PROJ).o $(CROSS)strip $(PROJ) clean: rm -f $(PROJ) $(PROJ).o 3. Step: Transfer the executable for the measurement Telnet server from your development system to the DNP/1110. You can use a FTP session for this file transfer. 4. Step: Run the measurement Telnet server on your DNP/1110. Make sure to have superuser rights. Then enter./msrtsvr6 5000 5000 is a TCP port number. You can use any other unused port number, but not 23. This is the standard port number for Telnet servers. Please note, that you have a other Telnet server currently running. This server is using the TCP port number 23. A good choice are port numbers over 1024. 5. Step: Now you can test the measurement Telnet server. Run a new Telnet client program on your development system. Then enter telnet 192.168.0.126 5000 within a text console window. After that a Telnet client for TCP port number 5000 starts and SSV EMBEDDED SYSTEMS 2002, mht-sa09.doc, Rev. 1.01. 7

builds a TCP connection. Within the new Telnet client window you see the sign-in message of the measurement Telnet server. Now you can enter valid numbers. Watch the LEDs of your DNP/EVA2 evaluation board. For disconnect the TCP connection between the Telnet server and the Telnet client, please use the key combination CTRL-D. SSV EMBEDDED SYSTEMS 2002, mht-sa09.doc, Rev. 1.01. 8