TCP/IP Application Development
From Computing and Software Wiki
(Difference between revisions)
(→Examples) |
|||
Line 8: | Line 8: | ||
==Examples== | ==Examples== | ||
+ | <code><pre> | ||
+ | package sockets; | ||
+ | |||
+ | 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){} | ||
+ | |||
+ | |||
+ | |||
+ | 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 | ||
+ | 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(); | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | </pre></code> | ||
+ | |||
+ | <code><pre> | ||
+ | package sockets; | ||
+ | |||
+ | |||
+ | |||
+ | 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(); | ||
+ | |||
+ | |||
+ | |||
+ | System.out.println("Local Port: "+myServer.getLocalPort()); | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | //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(); | ||
+ | |||
+ | |||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | |||
+ | </pre></code> | ||
+ | |||
<code><pre> | <code><pre> | ||
/* A simple client in C */ | /* A simple client in C */ | ||
Line 116: | Line 273: | ||
} | } | ||
</pre></code> | </pre></code> | ||
+ | |||
==Best Practices== | ==Best Practices== | ||
Revision as of 20:43, 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
package sockets;
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){}
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
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();
}
}
package sockets;
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();
System.out.println("Local Port: "+myServer.getLocalPort());
//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;
}