TCP/IP Application Development

From Computing and Software Wiki

Revision as of 21:14, 8 April 2008 by Hank3 (Talk)
Jump to: navigation, search

Introduction

TCP/IP Applications are Applications based on the TCP/IP protocol suite. It is part of the Application Layer of the Layered Network Model also known as OSI. Unlike UDP, TCP is a connection based protocol meaning two applications communicating with each must first establish a connection. A connection is established by TCP handshaking.

There are three essential information that is needed for a connection 1. Protocol(TCP) 2. IP address 3. Port number File:Http://www.hardwaresecrets.com/imageview.php?image=6731

Contents


Sockets

Client/Server Model

Examples in Java

/* Simple TCP/IP Client App */

import java.net.*;
import java.io.*;

public class SocketClient {
	public static void main(String args[]) throws IOException
	{
		Socket mySocket = null;
		BufferedReader in = null; //INPUT
		PrintWriter out = null; //OUTPUT
		
		//INIT AND GET SOCKET
		try{
		mySocket = new Socket("192.168.1.105", 9999);
		}catch(UnknownHostException e){}
		
		//INPUT STREAM
		in = new BufferedReader(new InputStreamReader(
                mySocket.getInputStream()));
		
		//OUTPUT STREAM
		out = new PrintWriter(mySocket.getOutputStream(), true);
		
		//USER INPUT STREAM
		BufferedReader stdIn = new BufferedReader(
                new InputStreamReader(System.in));
		
		//DO SOMETHING WITH STREAMS
		String userInput;
		while(true)
		{
			userInput = stdIn.readLine();
			out.println(userInput);
			if(userInput.equals("exit"))
			{
				break;
			}
		}
		
		
		//CLOSE STREAMS
		in.close();
		out.close();
		
		//CLOSE SOCKET
		mySocket.close();
		
	}

}
/* Simple TCP/IP Server App */
import java.net.*;
import java.io.*;

public class SocketServer {
    public static void main(String args[]) throws IOException
    {
        ServerSocket myServer = null;
        BufferedReader in = null; //INPUT
        PrintWriter out = null; //OUTPUT
        Socket mySocket = null;
       
        //INIT AND GET SOCKET
        try{
        myServer = new ServerSocket(9999);
        }catch(UnknownHostException e){}
       
        //LISTEN IN AND ACCEPT CONNECTIONS ON SOCKET
        mySocket = myServer.accept();
       
        //INPUT STREAM
        in = new BufferedReader(new InputStreamReader(
                mySocket.getInputStream()));
       
        //OUTPUT STREAM
        out = new PrintWriter(mySocket.getOutputStream(), true);
       
        //DO SOMETHING WITH STREAMS
        String inputFromClient = "";
        while((inputFromClient = in.readLine()) != null)
        {
        	System.out.println(inputFromClient);
        	if (inputFromClient.equals("exit"))
        	{
        		break;
        	}
        }
        
        //CLOSE STREAMS
        in.close();
        out.close();
       
        //CLOSE SOCKET AND SERVER
        myServer.close();
        mySocket.close();
    }
}

Examples in C

/* A simple client in C */

#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>

#define SERVERPORT 8888
#define SERVERADDRESS "192.168.1.105"

int main()
{
	int servSock, bytes_sent, sin_size;
	
	char msg[10];
	
	struct sockaddr_in serverAddress; // server's address and port information
	
	struct hostent *host; //host information
	
	
	if ((servSock = socket(PF_INET, SOCK_STREAM, 0)) < 0)
		printf("socket() FAILED");
		
	serverAddress.sin_family = AF_INET;
	serverAddress.sin_port = htons(SERVERPORT);
	serverAddress.sin_addr.s_addr = inet_addr(SERVERADDRESS);
	memset(serverAddress.sin_zero, '\0', sizeof serverAddress.sin_zero);
	
	connect(servSock, (struct sockaddr *)&serverAddress, sizeof serverAddress);
	
	while(1)
	{
		scanf("%s", msg);
		send(servSock, msg, sizeof (msg), 0);
		if(strcmp(msg, "exit") == 0)
			break;
	}
	close(servSock);
	
	return 1;
}
/* A simple server in C */

#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>

#define MYPORT 8888
#define BACKLOG 1

int main()
{
	int servSock, new_fd, sin_size, bytes_sent, bytes_recv;
	
	char buffer[10];
	
	struct sockaddr_in myServer; //server information
	struct sockaddr_in their_addr; // connector's address information
	
	if ((servSock = socket(PF_INET, SOCK_STREAM, 0)) < 0)
		printf("socket() FAILED");
	myServer.sin_family = AF_INET;
	myServer.sin_port = htons(MYPORT);
	myServer.sin_addr.s_addr = INADDR_ANY;
	memset(myServer.sin_zero, '\0', sizeof myServer.sin_zero);
	
	printf("Server [%d] accepting and listening on %d\n", myServer.sin_addr.s_addr, myServer.sin_port);
	
	bind(servSock, (struct sockaddr *)&myServer, sizeof myServer);
	
	
	
	listen(servSock, BACKLOG); //LISTEN IN PORT 8888
	
	sin_size = sizeof their_addr;
	
	printf("size = %d\n", sizeof (buffer));
	

		

new_fd = accept(servSock, (struct sockaddr *)&their_addr, &sin_size);
		while(1)
		{
			bytes_recv = recv(new_fd, buffer, sizeof (buffer), 0);
		//buffer[10] = '\0';
			printf("%s\n", buffer);
			if(strcmp(buffer, "exit") == 0)
				break;

		}
	
	close(new_fd);
	
	close(servSock);
	
	return 1;
}

Best Practices

Security Issues

References

[1] Catalyst Development Corporation, [1]

[2] Curran, Kevin, and Elaine Smyth. "Security Issues with Wi-Fi Networks." Encyclopedia of Internet Technologies and Applications, 2008: 498-504.

[5] Phifer, Lisa. "The Caffe Latte Attack: How It Works and How to Block It". December 12, 2007. (accessed March 26, 2008).

Personal tools