Saturday, 20 October 2012

Accept n numbers through one program send it to the server program check their for prime numbers send only the prime numbers to client-Java Socket program

tcpserver1.java

import java.io.*;
import java.net.*;
class tcpserver1
{
public static void main(String args[])throws Exception
{
ServerSocket ss = new ServerSocket(6856);
Socket cs=ss.accept();
BufferedReader br = new BufferedReader(new InputStreamReader(cs.getInputStream()));
PrintStream ps = new PrintStream(cs.getOutputStream());
int[] a=new int[15];
int[] b= new int [15];
int k=0,n,i,j,f=0;
n=Integer.parseInt(br.readLine());
for(i=0;i<n;i++)
a[i]=Integer.parseInt(br.readLine());
for(i=0;i<n;i++)
{
for(j=2;j<a[i];j++)
{
if(a[i]%j==0)
{
f=1;
break;
}
}
if(f==0)
{
if(a[i]!=1)
{
b[k]=a[i];
k++;
}
}
f=0;
}
ps.println(k);
for(i=0;i<k;i++)
ps.println(b[i]);

}
}

tcpclient1.java

import java.io.*;
import java.net.*;
class tcpclient1
{
public static void main(String args[])throws Exception
{
Socket cs = new Socket("localhost",6856);

BufferedReader kb = new BufferedReader(new InputStreamReader(System.in));
BufferedReader br = new BufferedReader(new InputStreamReader(cs.getInputStream()));
PrintStream ps = new PrintStream(cs.getOutputStream());
int[] a=new int[15];
int[] b= new int [15];
int k,n,i;
System.out.println("Enter the limit");
n=Integer.parseInt(kb.readLine());
ps.println(n);
System.out.println("Enter the Numbers");
for(i=0;i<n;i++)
a[i]=Integer.parseInt(kb.readLine());
for(i=0;i<n;i++)
ps.println(a[i]);
k=Integer.parseInt(br.readLine());
for(i=0;i<k;i++)
b[i]=Integer.parseInt(br.readLine());
System.out.println("Prime nos\n");
for(i=0;i<k;i++)
System.out.println(b[i]);

}
}

Communication between two unrelated process using messege queue

MSGQ1.c 

#include<stdio.h>
#include<sys/msg.h>
#include<string.h>
#include<stdlib.h>
struct msgbuf
{
long mtype;
char mtext[30];
}ptr;
int main()
{
int i=0;
int id=msgget(52,0666|IPC_CREAT);
if(id>=0)
printf("Msg Q created sucessfully\n");
else
printf("Not created\n");
printf("Enter the msg type:\n");
scanf("%ld",&(ptr.mtype));

printf("Enter the msg\n");
scanf("%s",ptr.mtext);
int p=msgsnd(id,&ptr,(sizeof(ptr.mtext)),0);
if(p>=0)
printf("Msg send sucessfully\n");
else
printf("Msg not send\n");
}




MSGQ2.C



#include<stdio.h>
#include<sys/msg.h>
struct msgbuf
{
long mtype;
char mtext[30];
}ptr;
int main()
{

int id=msgget(52,0666|IPC_CREAT);
if(id>=0)
printf("Success\n");
printf("Enter the msgtype\n");
scanf("%ld",&ptr.mtype);

int p=msgrcv(id,&ptr,30,(ptr.mtype),0);
if(p>=0)
{
printf("Msg read sucessfully\n");
printf("Msg is:%s\n",ptr.mtext);
}
else
printf("Error\n");
}


Sample output


msgq1.c

Enter the msg type:
2
Enter the msg
kkk
Msg send sucessfully


msgq2.c

Success
Enter the msgtype
2
Msg read sucessfully
Msg is:kkk





Sunday, 7 October 2012

Udp Client-Server Communication using java

UDP Client

import java.io.*;
import java.net.*;
class udpclient {
    public static void main(String args[])throws Exception
    {
       
        DatagramSocket cs = new DatagramSocket(2568);
        InetAddress ip = InetAddress.getByName("localhost");
        byte[] sD = new byte[1024];
        byte[] rD = new byte[1024];
 String sen;
        while(true)
        {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
       sen= br.readLine();
      
       
        sD=sen.getBytes();
        DatagramPacket sPkt = new DatagramPacket(sD,sD.length,ip,6800);
        cs.send(sPkt);
        DatagramPacket rPkt = new DatagramPacket(rD,rD.length);
        cs.receive(rPkt);
        String str= new String(rPkt.getData());
        System.out.println("From Server:\n"+str);
        }
       
    }
   
}

UDP Server

import java.io.*;
import java.net.*;
class udpserver {
    public static void main(String args[])throws Exception
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        DatagramSocket cs = new DatagramSocket(6800);
       
        byte[] sD = new byte[1024];
        byte[] rD = new byte[1024];
        while(true)
        {
          DatagramPacket rPkt = new DatagramPacket(rD,rD.length);
        cs.receive(rPkt);
        String str= new String(rPkt.getData());
        System.out.println("From Client:\n"+str);
InetAddress ip = rPkt.getAddress();
int port = rPkt.getPort();  
        String sen= br.readLine();
       
       
        sD=sen.getBytes();
        DatagramPacket sPkt = new DatagramPacket(sD,sD.length,ip,port);
        cs.send(sPkt);
      
        }
       
    }
   
}

Tcp client server communication using Java

TCP Client



import java.io.*;
import java.net.*;
 class tcpclient {
    public static void main(String args[])throws Exception
    {
        Socket s = new Socket("localhost",6700);
        BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
        PrintStream ps2 = new PrintStream(s.getOutputStream());
        BufferedReader kb = new BufferedReader(new InputStreamReader(System.in));
        String str1,str2;
        while(true)
        {

            str1=kb.readLine();
            ps2.println(str1+"\n");
            str2=br.readLine();
            System.out.println(str2);
           
        }

       
    }
   
}

TCP Server


import java.io.*;
import java.net.*;
import java.lang.*;
class tcpserver {
    public static void main(String args[])throws Exception
    {
        ServerSocket ss = new ServerSocket(6700);
        Socket s = ss.accept();
        BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
        BufferedReader kb = new BufferedReader(new InputStreamReader(System.in));
        PrintStream ps = new PrintStream(s.getOutputStream());

        String str1,str2;
        while(true)
        {
            str1=kb.readLine();
           
               ps.println(str1);
          str2=br.readLine();
                System.out.println(str2+"\n");
               
               
           
        }
       
       
       
       
    }
   
}

Two unrelated programs uses shared memory

First program


#include<stdio.h>
#include<sys/shm.h>
#include<string.h>
int main()
{

int id=shmget(17,50,0666|IPC_CREAT);


if(id>=0)
{
printf("\nShared memory created with id:%d\n",id);
}
char *adr=shmat(id,NULL,0);
if(adr>=0)
printf("Memory attached\n");
printf("Enter a string\n");
char str[10];

scanf("%s",str);
strncpy(adr,str,sizeof(str));



}



Second program

#include<stdio.h>
#include<sys/shm.h>
#include<string.h>
int main()
{

int id=shmget(17,0,0666|IPC_CREAT);
char str[10];

if(id>=0)
{
printf("\nShared memory with id:%d\n is accessed",id);
}
char *adr=shmat(id,NULL,0);
if(adr>=0)
printf("\nMemory attached\n");
printf("\nThe string is\n%s",adr);
}


Sample output:

cc first.c
./a.out

Shared memory created with id:360454
Memory attached
Enter a string
Hello

cc second.c
./a.out

Shared memory with id:360454
 is accessed
Memory attached

The string is
hello


Saturday, 19 May 2012

Make bootable pendrive for ubuntu(in windows platform)


It is very easy.
First download Universal-USB-Installer.
Just fix the locations of pendrive and ISO image.
Click create button.
That's it.

Download Universal- USB-Installer from here
                                                                            

Thursday, 17 May 2012

A method to make one click buttons in php

Here is a method to make buttons which will disabled automatically after click. This is useful when you dealing with php scripts.There also other methods for this. Here is only the logic not complete code.

<?php
mysql_select_db("sample", $p);
$z1=$z2=0;
$r=mysql_query("SELECT * FROM sampletable");

while($rows=mysql_fetch_array($r))
{
$take=$rows['samples2'];


if($take=='a1')
$z1=1;
if($take=='a2')
$z2=1;
}

if($z1==1)
echo "<input type='submit' value='a1'  name='opt' disabled >";
else
echo "<input type='submit' value='a1'  name='opt' >";




if($z2==1)
echo "<input type='submit' value='a2'  name='opt' disabled >";
else
echo "<input type='submit' value='a2'  name='opt'>";


mysql_close($p);

?>







Twitter Delicious Facebook Digg Stumbleupon Favorites More