TCP/IP Application Development

From Computing and Software Wiki

(Difference between revisions)
Jump to: navigation, search
(Examples)
(Examples)
Line 9: Line 9:
==Examples==
==Examples==
<code><pre>
<code><pre>
-
package sockets;
 
-
 
import java.net.*;
import java.net.*;
import java.io.*;
import java.io.*;
public class SocketClient {
public class SocketClient {
-
 
public static void main(String args[]) throws IOException
public static void main(String args[]) throws IOException
{
{
Line 26: Line 23:
mySocket = new Socket("192.168.1.105", 9999);
mySocket = new Socket("192.168.1.105", 9999);
}catch(UnknownHostException e){}
}catch(UnknownHostException e){}
-
 
-
 
-
 
-
System.out.println("Local Address: "+mySocket.getLocalAddress());
 
-
System.out.println("Local Port: "+mySocket.getLocalPort());
 
-
System.out.println("Port: "+mySocket.getPort());
 
-
System.out.println("Traffic Class: "+mySocket.getTrafficClass());
 
-
 
//INPUT STREAM
//INPUT STREAM
Line 72: Line 61:
<code><pre>
<code><pre>
-
package sockets;
 
-
 
-
 
-
 
import java.net.*;
import java.net.*;
-
 
import java.io.*;
import java.io.*;
-
 
-
 
public class SocketServer {
public class SocketServer {
public static void main(String args[]) throws IOException
public static void main(String args[]) throws IOException
-
 
{
{
-
 
ServerSocket myServer = null;
ServerSocket myServer = null;
-
 
BufferedReader in = null; //INPUT
BufferedReader in = null; //INPUT
-
 
PrintWriter out = null; //OUTPUT
PrintWriter out = null; //OUTPUT
-
 
Socket mySocket = null;
Socket mySocket = null;
-
 
-
 
//INIT AND GET SOCKET
//INIT AND GET SOCKET
-
 
try{
try{
-
 
myServer = new ServerSocket(9999);
myServer = new ServerSocket(9999);
-
 
}catch(UnknownHostException e){}
}catch(UnknownHostException e){}
-
 
-
 
//LISTEN IN AND ACCEPT CONNECTIONS ON SOCKET
//LISTEN IN AND ACCEPT CONNECTIONS ON SOCKET
-
 
+
mySocket = myServer.accept();
-
mySocket = myServer.accept();  
+
-
 
+
-
+
-
 
+
-
System.out.println("Local Port: "+myServer.getLocalPort());
+
-
 
+
-
+
-
 
+
-
+
-
 
+
-
+
//INPUT STREAM
//INPUT STREAM
-
 
in = new BufferedReader(new InputStreamReader(
in = new BufferedReader(new InputStreamReader(
-
 
                 mySocket.getInputStream()));
                 mySocket.getInputStream()));
-
 
-
 
//OUTPUT STREAM
//OUTPUT STREAM
-
 
out = new PrintWriter(mySocket.getOutputStream(), true);
out = new PrintWriter(mySocket.getOutputStream(), true);
-
 
-
 
//DO SOMETHING WITH STREAMS
//DO SOMETHING WITH STREAMS
-
 
out.write("HELLO WORLD!");
out.write("HELLO WORLD!");
-
 
//CLOSE STREAMS
//CLOSE STREAMS
-
 
in.close();
in.close();
-
 
out.close();
out.close();
-
 
//CLOSE SOCKET AND SERVER
//CLOSE SOCKET AND SERVER
-
 
myServer.close();
myServer.close();
-
 
mySocket.close();
mySocket.close();
-
 
-
 
-
 
}
}

Revision as of 20:45, 8 April 2008

Contents


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.

Client/Server Model

Examples

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();
		
	}

}
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
		out.write("HELLO WORLD!");
		

		//CLOSE STREAMS
		in.close();
		out.close();


		//CLOSE SOCKET AND SERVER
		myServer.close();
		mySocket.close();
	}

}


/* 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

See also

External links

Personal tools