Wednesday 5 September 2012

Getting System Date/Time from Server using TCP

Server Program:

import java.net.*;
import java.io.*;
import java.util.*;
public class DataServer
{
public static void main(String[] st) throws UnknownHostException, IOException
{
ServerSocket ss=new ServerSocket(1111);
System.out.println("Server Started");
System.out.println("Waiting for a client request");
Socket soc=ss.accept();
InputStreamReader isr=new InputStreamReader(soc.getInputStream());
BufferedReader br=new BufferedReader(isr);
String s=br.readLine();
System.out.println("The client request is:"+s);
OutputStreamWriter osw=new OutputStreamWriter(soc.getOutputStream());
PrintWriter pw=new PrintWriter(osw,true);
Calendar calender=Calendar.getInstance();
pw.println("System Date is");
pw.println("Date:"+calender.get(Calendar.DATE));
pw.println("Month:"+(calender.get(Calendar.MONTH)+1));
pw.println("Year:"+calender.get(Calendar.YEAR));
pw.println("System time is:");
pw.println("Hours:"+calender.get(Calendar.HOUR));
pw.println("Minutes:"+calender.get(Calendar.MINUTE));
pw.println("Seconds:"+calender.get(Calendar.SECOND));
pw.println("exit");
System.out.println("Reply sent to client");
soc.close();
}
}

Client Program:

import java.net.*;
import java.io.*;
import java.util.*;
public class DataClient
{
public static void main(String[] st) throws UnknownHostException, IOException
{
String s=" ";
InetAddress addr=InetAddress.getLocalHost();
Socket soc=new Socket(addr,1111);
System.out.println("client started");
System.out.println("Request sent to server");
OutputStreamWriter osw=new OutputStreamWriter(soc.getOutputStream());
PrintWriter pw=new PrintWriter(osw,true);
pw.println("Please send date & time");
InputStreamReader isr=new InputStreamReader(soc.getInputStream());
BufferedReader br= new BufferedReader(isr);
while(true)
{
s=br.readLine();
if(s.equals("exit"))break;
System.out.println(s);
}
soc.close();
}
}

Tuesday 4 September 2012

Echo Server using TCP

Echo Server Program :

import java.io.*;
import java.net.*;
public class echoserver
{
public static void main(String a[])throws UnknownHostException, IOException{
ServerSocket ss=new ServerSocket(1111);
Socket soc =ss.accept();
InputStreamReader isr=new InputStreamReader(soc.getInputStream());
BufferedReader br=new BufferedReader(isr);
while(true) 
{ 
String s=br.readLine();
if(s.equals("exit"))break;
OutputStreamWriter osw=new OutputStreamWriter(soc.getOutputStream());
PrintWriter pw=new PrintWriter(osw,true);
pw.println(s);
}
soc.close();
}}


Echo Client Program:

import java.io.*;
import java.net.*;
public class echoclient
{
public static void main(String args[]) throws IOException
{
try {
int Port;
BufferedReader Buf =new BufferedReader(new      InputStreamReader(System.in));
System.out.print(" Enter the Port Address : " );
Port=Integer.parseInt(Buf.readLine());
Socket sok=new Socket("localhost",Port);
if(sok.isConnected()==true)
System.out.println(" Server Socket is Connected Succecfully. ");
InputStream in=sok.getInputStream();
OutputStream ou=sok.getOutputStream();
PrintWriter pr=new PrintWriter(ou);
BufferedReader buf1=new BufferedReader(new
InputStreamReader(System.in));
BufferedReader buf2=new BufferedReader(new
InputStreamReader(in));
String str1,str2;
System.out.print(" Enter the Message : ");
str1=buf1.readLine();
pr.println(str1);
pr.flush();
System.out.println(" Message Send Successfully. ");
str2=buf2.readLine();
System.out.println(" Message From Server : " + str2);
     }
 catch(Exception e)
  {
 System.out.println(" Error : " + e.getMessage());
  }
}
}



Friday 17 August 2012

System Software program 3:


Symbol table creation:

Program:

#include<stdio.h>
#include<conio.h>
struct sym
{
char lab[10];
int val;
};
void main()
{
FILE *f1;
char la[10],op[10],opr[10],a[1000],c,key[10];
int i,j,k=0,m=0,flag,ch=0;
struct sym s[10];
clrscr();
f1=fopen("a1.txt","r");
c=fgetc(f1);
i=0;
printf("\n SOURCE PROGRAM \n");
while(c!=EOF)
{
a[i]=c;
c=fgetc(f1);
i++;
}
while(ch<4)
{
printf("1.Symbol table creation\n");
printf("2.search\n");
printf("3.display\n");
printf(">3.Exit\n");
printf("Enter your choice");
scanf("%d",&ch);
switch(ch)
{
case 1:
i=0;
while(strcmp(op,"end")!=0)
{
if(a[i]=='\t')
{
strcpy(la," ");
i++;
}
else
{
j=0;
while(a[i]!='\t')
{
la[j]=a[i];
i++;
j++;
}
la[j]='\0';
i++;
}
if(a[i]=='\t')
{
strcpy(op," ");
i++;
}
else
{
j=0;
while(a[i]!='\t')
{
op[j]=a[i];
i++;
j++;
}
op[j]='\0';
i++;
}
if(a[i]=='\t')
{
strcpy(opr," ");
i++;
}
else
{
j=0;
while(a[i]!='\n')
{
opr[j]=a[i];
i++;
j++;
}
opr[j]='\0';
i++;
}
j=0;
if(strcmp(la," ")!=0)
{
strcpy(s[m].lab,la);
if(strcmp(op,"start")==o)
{
k=atoi(opr);
s[m].val=k;
m++;
printf("%s\t%s\t%s\n",la,op,opr);
continue;
}
else if(strcmp(op,"equ")==0)
{
s[m].val=atoi(opr);
m++;
}
else if(strcmp(op,"resq")==0)
{
s[m].val=k;
k=k+atoi(opr)*3;
m++;
}
else if(strcmp(op,"resb")==0)
{
s[m].val=k;
k=k+atoi(opr);
m++;
}
else
{
s[m].val=k;
k=k+3;
m++;
}
}
else
k=k+3;
printf("%s\t%s\t%s\n",la,op,opr);
}
break;
case 2:
printf("Enter the label to be searched\n");
scanf("%s",&key);
flag=0;
for(i=0;i<m;i++)
{
if(strcmp(key,s[i].lab)==0)
{
printf("%s\t%d\n",s[i].lab,s[i].val);
flag=1;
break;
}
else
continue;
}
if(flag==0)
printf("Label not found\n");
break;
case 3:
printf("\n Symbol table \n");
for(i=0;i<m;i++)
printf("\n %s\t%d\n",s[i].lab,s[i].val);
break;
}
}
}


Input file:

copy start 1000
add lda one
** sta two
** ldch charz
** stch c1
one word 100
two resw 1
char byte c'z'
c1 resb 1
** end add

Output:

SOURCE PROGRAM
1.symbol table creation
2.search
3.display
>3.exit
Enter your choice 1
copy start 1000
add lda one
** sta two
** ldch charz
** stch c1
one word 100
two resw 1
charz byte c'z'
c1 resb 1
** end add
1.symbol table creation
2.search
3.display
>3.exit
Enter your choice 3
copy 1000
add 1000
** 1003
** 1006
** 1009
one 1012
two 1015
charz 1018
c1 1021
** 1022
1.symbol table creation
2.search
3.display
>3.exit
Enter your choice 2
Enter the label to be searched
two
two 1015
1.symbol table creation
2.search
3.display
>3.exit
Enter your choice 4

System Software program 2:


Implementation of Two pass of a two pass Assembler:

Program:

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
void main()
{
char a[10],ad[10],label[10],opcode[10],operand[10],mnemonic[10],symbol[10];
int i, address,code,add,len,actuallen;
FILE *fp1,*fp2,*fp3,*fp4;
clrscr();
fp1=fopen("AssmList.txt","w");
fp2=fopen("symtab.txt","r");
fp3=fopen("intermediate.txt","r");
fp4=fopen("optab.txt","r");
fscanf(fp3,"%s%s%s",label,opcode,operand);
if(strcmp(opcode,"start")==0)
{
fprintf(fp1,"\t%s\t%s\t%s\n",label,opcode,operand);
fscanf(fp3,"%d%s%s%s",&address,label,opcode,operand);
}
while(strcmp(opcode,"end")!=0)
{
if(strcmp(opcode,"byte")==0)
{
fprintf(fp1,"%d\t%s\t%s\t%s\t",address,label,opcode,operand);
len=strlen(operand);
actuallen=len-3;
for(i=2;i<(actuallen+2);i++)
{
itopa(operand[i],ad,16);
fprintf(fp1,"%s",ad);
}
fprintf(fp1,"\n");
}
else if(strcmp(opcode,"word")==0)
{
len=strlen(operand);
itoa(atoi(operand),a,10);
fprintf(fp1,"%d\t%s\t%s\t%s\t00000%s\n",address,label,opcode,operand,a);
}
else if((strcmp(opcode,"resb")==0)||(strcmp(opcode,"resw")==0))
{
fprintf(fp1,"%d\t%s\t%s\t%s\n",address,label,opcode,operand);
}
else
{
rewind(fp4);
fscanf(fp4,"%s%d",mnemonic,&code);
while(strcmp(opcode,mnemonic)!=0)
fscanf(fp4,"%s%d",mnemonic,&code);
if(strcmp(operand,"**")==0)
{
fprintf(fp1,"%d\t%s\t%s\t0000%d\n",address,label,opcode,operand,code);
}
else
{
rewind(fp2);
fscanf(fp2,"%s%d",symbol,&add);
while(strcmp(operand,symbol)!=0)
fscanf(fp2,"%s%d",symbol,&code);
}
fprintf(fp1,"%d\t%s\%s\t%s\t%d\n",address,label,opcode,operand,code);
}
}
fscanf(fp3,"%d%s%s%s",&address,label,opcode,operand);
}
fprintf(fp1,"%d\t%s\t%s\t%s\t",address,label,opcode,operand);
printf("FINISHED");
fclose(fp1);
fclose(fp2);
fclose(fp3);
fclose(fp4);
getch();
}

System software program 1 :



Implementation of one pass of a two pass assembler:


program:

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char opcode[10],mnemonic[10],operand[10],label[10],code[10];
int locctr,start,length;
FILE *fp1,*fp2,*fp3,*fp4;
clrscr();
fp1=fopen("Input.dat","r");
fp2=fopen("Symtab.dat","w");
fp3=fopen("Out.dat","w");
fp4=fopen("optab.dat","r");
fscanf(fp1,"%s%s%s",label,opcode,operand);
if(strcmp(opcode,"Start")==0)
{
start=atoi(operand);
locctr=start;
fprintf(fp3,"%s\t%s\t%s\n",label,opcode,operand);
fscanf(fp1,"%s%s%s",label,opcode,operand);
}
else
locctr=start;
while(strcmp(opcode,"END")!=0)
{
fprintf(fp3,"%d\t",locctr);
if(strcmp(label,"**")!=0)
fprintf(fp2,"%d\t%d\n",locctr,label);
recoind(fp4)
fscanfa(fp4,"%s",mnemonic);
while(strcmp(mnemonic,"END")!=0)
{
if(strcmp(opcode,mnemonic)==0)
{
locctr+=3;
break;
}
fscanf(fp4,"%s",mnemonic);
}
if(strcmp(opcode,"WORD")==0)
locctr+=3;
else if(strcmp(opcode,"RESW")==0)
locctr+=(3*(atoi(operand)));
else if(strcmp(opcode,"BYTE")==0)
++locctr;
fprintf(fp3,"%s\t%s\t%s\n",label,opcode,operand);
fscanf(fp1,"%s%s%s",label,opcode,operand);
}
fprintf(fp3,"%d\t%s\t%s\t%s\n",locctr,label,opcode,operand);
length=locctr-start;
printf("\nThe length of the program is %d",length);
fclose(fp1);
fclose(fp2);
fclose(fp3);
fclose(fp4);
getch();
}

Input file:

input.txt

** start 2000
** LDA five
** STA five
** LDCH charz
** STCH c1
alpha RESW 1
five WORD 1
charz BYTE c'z'
c1 RESB 1
** END **

optab.txt

Start
LDA
STA
LDCH
STCH
END

Output file:

symtab.txt

alpha 2012
five 2015
charz 2018
c1 2019

out.txt

** start 2000
2000 ** LDA five
2003 ** STA five
2006 ** LDCH charz
2009 ** STCH c1
2012 alpha RESW 1
2015 five WORD 5
2018 char BYTE c'z'
2019 c1 RESB 1
2019 ** END **

output:

The length of the program is 19

Hi frnds..........

This is my first blog......