Appendix B: NET_CON Source Code

Similar documents
Assignment description: This is a C++ project. The comms class containing the

CSCE 463/612 Networks and Distributed Processing Spring 2017

Winsock Server adding Multiple clients support C++

ECE 435 Network Engineering Lecture 2

CS321: Computer Networks Socket Programming

EE 122: Sockets. Motivation. Sockets. Types of Sockets. Kevin Lai September 11, 2002

>>> SOLUTIONS <<< Answer the following questions regarding the basics principles and concepts of networks.

Security impact of noexcept

Interprocess Communication Mechanisms

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

Interprocess Communication Mechanisms

CSC209H Lecture 9. Dan Zingaro. March 11, 2015

Internet protocol stack. Internetworking II: Network programming. April 20, UDP vs TCP. Berkeley Sockets Interface.

Internetworking II: Network programming. April 20, 2000

Introduction to Berkeley Sockets

Network Programming November 3, 2008

ECE 435 Network Engineering Lecture 2

Introduction to Berkeley Sockets

CSC209H Lecture 10. Dan Zingaro. March 18, 2015

>>> SOLUTIONS <<< Answer the following questions regarding the basics of computer networks and the Internet.

Introduction to Berkeley Sockets

CSCI 415 Computer Networks Homework 2 Due 02/13/08

CS321: Computer Networks Introduction to Application Layer

Introduction to Client-Server Model

CS 640: Computer Networking

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

2007 Microsoft Corporation. All rights reserved.

Title: SEH Overwrites Simplified v1.01. Date: October 29 th Author: Aelphaeis Mangarae

Project 3. Reliable Data Transfer over UDP. NTU CSIE Computer Networks 2011 Spring

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

System Programming. Sockets: examples

Expires: April 20, 2006 M. Tuexen Muenster Univ. of Applied Sciences T. Dreibholz University of Duisburg-Essen October 17, 2005

CSE 333 Section 8 - Client-Side Networking

ICT 6544 Distributed Systems Lecture 5

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

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

The BSD UNIX Socket Interface (CS 640 Lecture) Assignment 1. Interprocess Communication (IPC) Work Individually (no groups)

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

CSE 333 SECTION 7. Client-Side Network Programming

A. Basic Function Calls for Network Communications

EECS 123: Introduction to Real-Time Distributed Programming

StoneGate Firewall/VPN 2.5 TECHNICAL NOTE. Server Pool Monitoring Agent Protocol

Chapter 6. The Transport Layer. Transport Layer 3-1

A Socket Example. Haris Andrianakis & Angelos Stavrou George Mason University

C Structures in Practice

31 ChamSys Remote Protocol Commands

Cisco CallManager 4.0(1) AXL API Programming Guide

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

I/O Models. Kartik Gopalan

Unix Network Programming Chapter 4. Elementary TCP Sockets 광운대학교컴퓨터과학과 정보통신연구실 석사과정안중현

EECS122 Communications Networks Socket Programming. Jörn Altmann

Hyo-bong Son Computer Systems Laboratory Sungkyunkwan University

CSE 333 SECTION 6. Networking and sockets

CS 3516: Computer Networks

Dr. Tim Lin ECE Department Cal Poly Pomona

Signal Example 1. Signal Example 2

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

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

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

Introduction to Computer Networks

Network Games Part II. Architecture of Network Game

Group-A Assignment No. 6

CONCURRENCY MODEL. UNIX Programming 2014 Fall by Euiseong Seo

Windows Sockets: A Quick And Dirty Primer

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

Network Programming / : Introduc2on to Computer Systems 21 st Lecture, April. 4, Instructors: Todd Mowry and Anthony Rowe

I/O Multiplexing. Dec 2009

Socket Programming 2007/03/28

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

JFx Joystick. June 21, Unrestricted Distribution. Revision 1.00

STD_CU43USBSW_V1.0E. CUnet (MKY43) USB Unit. CU-43USB Software Manual

Socket Programming for TCP and UDP

Lab 0. Yvan Petillot. Networks - Lab 0 1

Sockets 15H2. Inshik Song

l27 handout.txt buggy server.c Printed by Michael Walfish Apr 29, 10 13:41 Page 1/1 Apr 29, 10 11:51 Page 1/1

Ports under 1024 are often considered special, and usually require special OS privileges to use.

Sockets. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

CSE 333 SECTION 7. Client-Side Network Programming

Ά η η 1 (30%): Sockets. socket () bind () listen () accept () connect () read () write () close ()

Piotr Mielecki Ph. D.

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

CS 43: Computer Networks. 05: Socket Programming September 12-14, 2018

How to write a Measurement Telnet Server

CLIENT-SIDE PROGRAMMING

Client software design

CS307 Operating Systems Processes

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

GS2K Throughput Measurement Application Note NT11602A Rev

Inter-Process Communication. Disclaimer: some slides are adopted from the book authors slides with permission 1

Final Exam Introduction to Computer Systems. May 3, Name: Andrew User ID: Recitation Section: This is an open-book exam.

CS118 Discussion 1B, Week 1. Taqi Raza BUNCHE 1209B, Fridays 12:00pm to 1:50pm

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

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

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

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

CSE 333 SECTION 8. Sockets, Network Programming

CWRU High-Speed Flexible Parts Feeder. Software Source Code

Defending Computer Networks Lecture 2: Vulnerabili0es. Stuart Staniford Adjunct Professor of Computer Science

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

New York University CSCI-UA : Advanced Computer Systems: Spring 2016 Midterm Exam

Transcription:

B Appendix B: NET_CON Source Code NET_CON.CPP / LSA1000 Sample Network Connection Copyright (c) 1998 LeCroy Corporation Written By: Ricardo Palacio April, 1998 / / $Header:$ $Log:$ / #include <windows.h> #include <winsock.h> #include <stdio.h> #include <time.h> #include "net_con.h" static int hsocket; static int stimeout = 3; static int swinsockinitflag = FALSE; static char scurrentaddress[256]; static int sconnectedflag = FALSE; #define CMD_BUF_LEN 8192 static char scommandbuffer[cmd_buf_len]; int TCP_Connect(char ip_address) SOCKADDR_IN serveraddr; B 1

Appendix B int sockaddrsize = sizeof (SOCKADDR), result; WORD wversionrequested; WSADATA wsadata; const int resp = 1; char tmpstr[512]; fd_set wr_set = 1, 0; TIMEVAL tval; unsigned long argp; if (sconnectedflag==true) strcpy(scurrentaddress, ip_address); tval.tv_sec = stimeout; tval.tv_usec = 0; if (!swinsockinitflag) wversionrequested = MAKEWORD(1, 1); if (WSAStartup(wVersionRequested, &wsadata)!= 0) MessageBox(0, "Unable to initialize the Windows socket environment.", "ERROR", MB_OK); swinsockinitflag = TRUE; / build server socket address / serveraddr.sin_family = AF_INET; serveraddr.sin_port = htons (SERVER_PORT); if ((serveraddr.sin_addr.s_addr = inet_addr(ip_address)) == -1) MessageBox(0, "Bad server address", "ERROR", MB_OK); / create client's socket / B 2

NET_CON Source Code if ((hsocket = socket(af_inet, SOCK_STREAM, 0)) == INVALID_SOCKET) MessageBox(0, "Unable to create client's socket.", "ERROR", MB_OK); if (setsockopt(hsocket, IPPROTO_TCP, TCP_NODELAY, (char)&resp, sizeof(resp))!= 0) MessageBox(0, "Unable to set socket option to TCP_NODELAY", "ERROR", MB_OK); wr_set.fd_array[0] = hsocket; argp = 1;//non blocking mode ioctlsocket(hsocket, FIONBIO, &argp); connect(hsocket, (SOCKADDR FAR ) &serveraddr, sockaddrsize); result = select(hsocket, NULL, &wr_set, NULL, &tval); argp = 0;//blocking mode ioctlsocket(hsocket, FIONBIO, &argp); / connect to server (scope) / if (result < 1) sprintf(tmpstr, "Unable to make connection to IP:%s", ip_address); MessageBox(0, tmpstr, "TIMEOUT", MB_OK); sconnectedflag = TRUE; B 3

Appendix B int TCP_Disconnect(void) if (sconnectedflag!= TRUE) closesocket(hsocket); sconnectedflag = FALSE; int TCP_WriteDevice(char buf, int len, BOOL eoi_flag) TCP_HEADER header; int result, bytes_more, bytes_xferd; char idxptr; if (sconnectedflag!= TRUE) if (len < CMD_BUF_LEN) strcpy(scommandbuffer, buf); // set the header info header.beoi_flag = DATA_FLAG; header.beoi_flag = (eoi_flag)? EOI_FLAG:0; header.reserved[0] = 1; header.reserved[1] = 0; header.reserved[2] = 0; header.ilength = htonl(len); // write the header first B 4

NET_CON Source Code if (send(hsocket, (char ) &header, sizeof(tcp_header), 0)!= sizeof(tcp_header)) MessageBox(0, "Unable to send header info to the server.", "ERROR", MB_OK); bytes_more = len; idxptr = buf; bytes_xferd = 0; while (1) // then write the rest of the block idxptr = buf + bytes_xferd; MB_OK); if ((result = send(hsocket, (char ) idxptr, bytes_more, 0)) < 0) MessageBox(0, "Unable to send data to the server.", "ERROR", bytes_xferd += result; bytes_more -= result; if (bytes_more <= 0) int TCP_ClearDevice(void) if (sconnectedflag!= TRUE) B 5

Appendix B TCP_Disconnect(); TCP_Connect(sCurrentAddress); int TCP_ReadDevice(char buf, int len, int recv_count) TCP_HEADER header; char tmpstr[512]; int result, accum, space_left, bytes_more, buf_count; char idxptr; fd_set rd_set = 1, 0; TIMEVAL tval; if (sconnectedflag!= TRUE) recv_count = 0; if (buf==null) rd_set.fd_array[0] = hsocket; tval.tv_sec = stimeout; tval.tv_usec = 0; memset(buf, 0, len); buf_count = 0; space_left = len; while (1) // block here until data is received of timeout expires B 6

NET_CON Source Code result = select(hsocket, &rd_set, NULL, NULL, &tval); if (result < 1) TCP_ClearDevice(); MessageBox(0, scommandbuffer, "Read timeout", MB_OK); // get the header info first accum = 0; while (1) memset(&header, 0, sizeof(tcp_header)); if ((result = recv(hsocket, (char ) &header + accum, sizeof(header) - accum, 0)) < 0) TCP_ClearDevice(); MessageBox(0, "Unable to receive header info from the server.", "ERROR", MB_OK); accum += result; if (accum>=sizeof(header)) header.ilength = ntohl(header.ilength); if (header.ilength < 1) len); // only read to len amount if (header.ilength > space_left) header.ilength = space_left; sprintf(tmpstr, "Read buffer size (%d bytes) is too small", MessageBox(0, tmpstr, "ERROR", MB_OK); // read the rest of the block accum = 0; while (1) idxptr = buf + (buf_count + accum); bytes_more = header.ilength - accum; if ((space_left-accum) < TCP_MINIMUM_PACKET_SIZE) B 7

Appendix B TCP_ClearDevice(); sprintf(tmpstr, "Read buffer needs to be adjusted, must be minimum of %d bytes", TCP_MINIMUM_PACKET_SIZE); MessageBox(0, tmpstr, "ERROR", MB_OK); if ((result = recv(hsocket, (char ) idxptr, (bytes_more>2048)?2048:bytes_more, 0)) < 0) TCP_ClearDevice(); MessageBox(0, "Unable to receive data from the server.", "ERROR", MB_OK); accum += result; if (accum >= header.ilength) if ((accum + buf_count) >= len) buf_count += accum; space_left -= accum; if (header.beoi_flag & EOI_FLAG) if (space_left <= 0) recv_count = buf_count; int TCP_SetTimeout(int seconds) B 8

NET_CON Source Code stimeout = seconds; int main(int argc, char argv[]) char replybuf[512]; int read; if (argc < 2) printf("\nexample: net_con 172.28.11.22\n"); if (TCP_Connect(argv[1])) if (TCP_WriteDevice("idn?\n", 6, TRUE)) TCP_Disconnect(); if (TCP_ReadDevice(replyBuf, 512, &read)) TCP_Disconnect(); if (TCP_Disconnect()) printf("scope's reply: %s\n", replybuf); B 9

Appendix B #define SERVER_PORT 1861 #define EOI_FLAG #define SRQ_FLAG #define CLEAR_FLAG #define LOCKOUT_FLAG #define REMOTE_FLAG #define DATA_FLAG 0x01 0x08 0x10 0x20 0x40 0x80 #define READ_TIME_OUT 10 #define TCP_MINIMUM_PACKET_SIZE 64 typedef struct unsigned char beoi_flag; unsigned char reserved[3]; int ilength; TCP_HEADER; B 10

B Appendix B: NET_CON Source Code B 1