Вы находитесь на странице: 1из 13

24.EXP NAME: Write a socket program to get the current date and time from the server.

SERVER:
SERVER:
#include"netinet/in.h"
#include"sys/socket.h"#include"stdio.h"
#include"string.h"
#include"time.h"
main( )
{
struct sockaddr_in sa;
struct sockaddr_in cli;int sockfd,conntfd;int len,ch;char str[100];
time_t tick;
sockfd=socket(AF_INET,SOCK_STREAM,0);
if(sockfd<0)
{
printf("error in socket\n");
exit(0);
}
else printf("Socket opened");
bzero(&sa,sizeof(sa));
sa.sin_port=htons(5600);
sa.sin_addr.s_addr=htonl(0);
if(bind(sockfd,(struct sockaddr*)&sa,sizeof(sa))<0)
{
printf("Error in binding\n");
}
else
printf("Binded Successfully");

listen(sockfd,50);
for(;;)
{
len=sizeof(ch);
conntfd=accept(sockfd,(struct sockaddr*)&cli,&len);
printf("Accepted");
tick=time(NULL);
snprintf(str,sizeof(str),"%s",ctime(&tick));
printf("%s",str);write(conntfd,str,100);
}
}

CLIENT:
CLIENT:
#include"netinet/in.h"
#include"sys/socket.h"
#include"stdio.h"
main()
{
struct sockaddr_in sa,cli;
int n,sockfd;
int len;char buff[100];
sockfd=socket(AF_INET,SOCK_STREAM,0);
if(sockfd<0){ printf("\nError in Socket");
exit(0);
}
else printf("\nSocket is Opened");
bzero(&sa,sizeof(sa));

sa.sin_family=AF_INET;
sa.sin_port=htons(5600);
if(connect(sockfd,(struct sockaddr*)&sa,sizeof(sa))<0)
{
printf("\nError in connection failed");
exit(0);
}
else
printf("\nconnected successfully");
if(n=read(sockfd,buff,sizeof(buff))<0)
{
printf("\nError in Reading");
exit(0);
}
else
{printf("\nMessage Read %s",buff);
}}
OUTPUT:
SERVER SIDE:
[shanthi@linuxserver ~]$ cc tcpcd.c
[shanthi@linuxserver ~]$ cc tcpcd.c -o ll
[shanthi@linuxserver ~]$ ./ll
Socket is Openedconnected successfully
Message Read Fri Sep 24 15:16:26 2010
[shanthi@linuxserver ~]$
CLIENT SIDE:
[shanthi@linuxserver ~]$ cc tcpd.c
[shanthi@linuxserver ~]$ cc tcpd.c -o kk
[shanthi@linuxserver ~]$ ./kk
SocketOpenedBinded SuccessfullyAccepted
Fri Sep 24 15:16:26 2010

25.EXP NAME: Write a socket program where the client will send lowercase letters and the
server will return uppercase letter.

SERVER:
/******************* SERVER CODE *****************/

#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <stdlib.h>

int main(){
int welcomeSocket, newSocket, portNum, clientLen, nBytes;
char buffer[1024];
struct sockaddr_in serverAddr;
struct sockaddr_storage serverStorage;
socklen_t addr_size;
int i;

welcomeSocket = socket(PF_INET, SOCK_STREAM, 0);

portNum = 7891;

serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(portNum);
serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
memset(serverAddr.sin_zero, '\0', sizeof serverAddr.sin_zero);

bind(welcomeSocket, (struct sockaddr *) &serverAddr, sizeof(serverAddr));

if(listen(welcomeSocket,5)==0)
printf("Listening\n");
else
printf("Error\n");

addr_size = sizeof serverStorage;

/*loop to keep accepting new connections*/


while(1){
newSocket = accept(welcomeSocket, (struct sockaddr *) &serverStorage, &addr_size);
/*fork a child process to handle the new connection*/
if(!fork()){
nBytes = 1;
/*loop while connection is live*/
while(nBytes!=0){
nBytes = recv(newSocket,buffer,1024,0);

for (i=0;i<nBytes-1;i++){
buffer[i] = toupper(buffer[i]);
}

send(newSocket,buffer,nBytes,0);
}
close(newSocket);
exit(0);

}
/*if parent, close the socket and go back to listening new requests*/
else{
close(newSocket);
}
}

return 0;
}

CLIENT:
/******************* CLIENT CODE *****************/

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

int main(){
int clientSocket, portNum, nBytes;
char buffer[1024];
struct sockaddr_in serverAddr;
socklen_t addr_size;

clientSocket = socket(PF_INET, SOCK_STREAM, 0);

portNum = 7891;

serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(portNum);
serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
memset(serverAddr.sin_zero, '\0', sizeof serverAddr.sin_zero);

addr_size = sizeof serverAddr;


connect(clientSocket, (struct sockaddr *) &serverAddr, addr_size);

while(1){
printf("Type a sentence to send to server:\n");
fgets(buffer,1024,stdin);
printf("You typed: %s",buffer);

nBytes = strlen(buffer) + 1;

send(clientSocket,buffer,nBytes,0);

recv(clientSocket, buffer, 1024, 0);

printf("Received from server: %s\n\n",buffer);


}

return 0;
}

26.EXP NAME: Write a server and a client program to implement TCP chat server-client.
SERVER:
SERVER
#include<stdio.h>
#include<netinet/in.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netdb.h>
#include<stdlib.h>
#include<string.h>
#define MAX 80
#define PORT 43454
#define SA struct sockaddr
void func(int sockfd)
{
char buff[MAX];
int n;
for(;;)
{
bzero(buff,MAX);
read(sockfd,buff,sizeof(buff));
printf("From client: %s\t To client : ",buff);
bzero(buff,MAX);
n=0;
while((buff[n++]=getchar())!='\n');
write(sockfd,buff,sizeof(buff));
if(strncmp("exit",buff,4)==0)
{

printf("Server Exit...\n");
break;
}
}
}
int main()
{
int sockfd,connfd,len;
struct sockaddr_in servaddr,cli;
sockfd=socket(AF_INET,SOCK_STREAM,0);
if(sockfd==-1)
{
printf("socket creation failed...\n");
exit(0);
}
else
printf("Socket successfully created..\n");
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
servaddr.sin_port=htons(PORT);
if((bind(sockfd,(SA*)&servaddr, sizeof(servaddr)))!=0)
{
printf("socket bind failed...\n");
exit(0);
}
else
printf("Socket successfully binded..\n");

if((listen(sockfd,5))!=0)
{
printf("Listen failed...\n");
exit(0);
}
else
printf("Server listening..\n");
len=sizeof(cli);
connfd=accept(sockfd,(SA *)&cli,&len);
if(connfd<0)
{
printf("server acccept failed...\n");
exit(0);
}
else
printf("server acccept the client...\n");
func(connfd);
close(sockfd);
}

CLIENT:
#include<stdio.h>
#include<netinet/in.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netdb.h>
#include<string.h>
#include<stdlib.h>

#define MAX 80
#define PORT 43454
#define SA struct sockaddr
void func(int sockfd)
{
char buff[MAX];
int n;
for(;;)
{
bzero(buff,sizeof(buff));
printf("Enter the string : ");
n=0;
while((buff[n++]=getchar())!='\n');
write(sockfd,buff,sizeof(buff));
bzero(buff,sizeof(buff));
read(sockfd,buff,sizeof(buff));
printf("From Server : %s",buff);
if((strncmp(buff,"exit",4))==0)
{
printf("Client Exit...\n");
break;
}
}
}

int main()
{
int sockfd,connfd;

struct sockaddr_in servaddr,cli;


sockfd=socket(AF_INET,SOCK_STREAM,0);
if(sockfd==-1)
{
printf("socket creation failed...\n");
exit(0);
}
else
printf("Socket successfully created..\n");
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr=inet_addr("127.0.0.1");
servaddr.sin_port=htons(PORT);
if(connect(sockfd,(SA *)&servaddr,sizeof(servaddr))!=0)
{
printf("connection with the server failed...\n");
exit(0);
}
else
printf("connected to the server..\n");
func(sockfd);
close(sockfd);
}
OUTPUT:
$ cc tcpchatserver.c $ ./a.out
Socket successfully created..
Socket successfully binded.. S
erver listening..

server acccept the client...


From client: hai
To client : hello
From client: exit To client : exit Server Exit...
$ CLIENT SIDE $ cc tcpchatclient.c $ ./a.out
Socket successfully created.. connected to the server..
Enter the string : hai
From Server : hello
Enter the string : exit From Server : exit Client Exit... $

Вам также может понравиться