TCP/IP Application Development

From Computing and Software Wiki

(Difference between revisions)
Jump to: navigation, search
(Examples)
Line 6: Line 6:
==Client/Server Model==
==Client/Server Model==
-
 
-
==Best Practices==
 
-
 
-
==Security Issues==
 
==Examples==
==Examples==
<code><pre>
<code><pre>
 +
/* A simple client in C */
 +
#include <string.h>
#include <string.h>
#include <sys/types.h>
#include <sys/types.h>
Line 56: Line 54:
}
}
</pre></code>
</pre></code>
 +
<code><pre>
 +
/* 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;
 +
}
 +
</pre></code>
 +
==Best Practices==
 +
 +
==Security Issues==
==References==
==References==

Revision as of 20:41, 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

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