0% found this document useful (0 votes)
149 views6 pages

Assignment No.1 1. Objective

The document describes a Java program that allows sending text messages between two computers until the word "exit" is entered. The server program uses a ServerSocket to listen for incoming connections on port 2000. The client program connects to the server and writes messages to the output stream, flushing after each message. Messages are read from input and displayed on the server until the client enters "exit" to close the connection. This provides a simple messaging system between two computers on a network.

Uploaded by

api-3816734
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
149 views6 pages

Assignment No.1 1. Objective

The document describes a Java program that allows sending text messages between two computers until the word "exit" is entered. The server program uses a ServerSocket to listen for incoming connections on port 2000. The client program connects to the server and writes messages to the output stream, flushing after each message. Messages are read from input and displayed on the server until the client enters "exit" to close the connection. This provides a simple messaging system between two computers on a network.

Uploaded by

api-3816734
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1

ASSIGNMENT NO.1

1. OBJECTIVE :-
To obtain the shortest distance between every two pair of
vertices of the following weighted graph.

2. SOURCE CODE :-
#include<stdio.h>
#include<conio.h>
#define inf 9999
typedef struct
{
int val;
int status;
}graph;
int min(int,int);

void main()
{
clrscr();
int a[20][20],n,i,j,h,s,d,start,end,p,c=0;
char ch;
graph g[10][10];
int flag[10],path[10];
clrscr();
printf("Enter the number of vertices: ");
scanf("%d",&n);
for(i=0;i<n;i++)
a[i][i]=0;
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
if(i!=j)
{
printf("\nVertex %d to Vertex %d:\n",i+1,j+1);
printf("Is there any edge(y/n)? ");
fflush(stdin);ch=getche();
printf("\n");
if(ch=='y' || ch=='Y')
a[i][j]=1;
else
a[i][j]=inf;
a[j][i]=a[i][j];
}
}
printf("\nEnter the starting vertex : ");
scanf("%d",&s);start=s-1;
printf("Enter the destination vertex : ");
scanf("%d",&d);end=d-1;
for(i=0;i<n;i++)
{
g[0][i].val=inf;
g[0][i].status=0;
flag[i]=0;
}
g[0][start].val=0;g[0][start].status=1;flag[start]=1;
for(i=1;flag[end]!=1;i++)
{
for(j=0,h=inf;j<n;j++)
{
if(flag[j]==0)
2

{
g[i][j].val=min(g[i-1][j].val,g[i-
1][start].val+a[start][j]);
if(g[i][j].val<=h)
{
h=g[i][j].val;
p=j;
}
}
else
g[i][j].val=g[i-1][j].val;
}
g[i][p].status=1;
flag[p]=1;
start=p;
}
printf("\n\nShortest Path length = %d",g[i-1][end].val);
path[c++]=end;p=end;
for(j=i-2;j>=0;j--)
if(g[j][p].val!=g[j+1][p].val)
{
for(int k=0;k<n && g[j][k].status!=1;k++);
path[c++]=k;p=k;
}
printf("\nShortest Path: ");
for(i=c-1;i>=0;i--)
printf("V%d,",path[i]+1);
getch();
}

int min(int x,int y)


{
return((x<y)?x:y);
}

3. OUTPUT
Enter the number of vertices: 4

Vertex 1 to Vertex 2:
Is there any edge(y/n)? y
Vertex 1 to Vertex 3:
Is there any edge(y/n)? n
Vertex 1 to Vertex 4:
Is there any edge(y/n)? y
Vertex 2 to Vertex 3:
Is there any edge(y/n)? y
Vertex 2 to Vertex 4:
Is there any edge(y/n)? n
Vertex 3 to Vertex 4:
Is there any edge(y/n)? y
Enter the starting vertex : 1
Enter the destination vertex : 3

Shortest Path length = 2


Shortest Path: V1,V4,V3

4. DISCUSSIONS :-
This algorithm uses the Dijkstra’s method to find the shortest path.

*********************
3

ASSIGNMENT NO.2

1. OBJECTIVE :- Program in java to find IP of a computer.

2. SOURCE CODE :-
import [Link];
import [Link].*;
import [Link].*;
import [Link];
public class IpTrap{
public static void main(String args[]){
try{
InetAddress addr = [Link](args[0]);
[Link]("IP Address:: "+addr);
[Link]("Host Name ::" +[Link]());
}
catch(Exception e){
[Link]("Can’t detect local host:: "+e);
}
}
}

3. OUTPUT :-

3.1 V1
[Link][Link] cannot resolve symbol
symbol : class InetAddress
location : class IpTrap
InetAddress localaddr = [Link]();
^
3.2 V2
[Link][Link] cannot resolve symbol
symbol : variable InetAddress
location : class IpTrap
InetAddress localaddr = [Link]();

3.2 V3
F:\Assignments\Java>java IpTrap home-3bb4cb7b40
IP Address:: home-3bb4cb7b40/[Link]
Host Name ::home-3bb4cb7b40

****************************
4

ASSIGNMENT NO. 3

1. OBJECTIVE :- Write a program in Java to send a text message to


another computer.

2. SOURCE CODE :-
//Server Program
import [Link].*;
import [Link].*;
import [Link].*;
public class server {
public static void main(String[] args) throws IOException {
ServerSocket s=new ServerSocket(2000);
[Link]("Server Starting.........");
Socket sock=[Link]();
BufferedReader datain=new BufferedReader(new
InputStreamReader([Link]()));
[Link]("Incoming message to Server :
"+[Link]());
[Link]();
[Link]();
}
}

//Client Program
import [Link].*;
import [Link].*;
import [Link].*;
public class client {
public static void main(String[] args) throws IOException{
Socket sock=new Socket([Link]("home-
3bb4cb7b40"),2000);
String message="Client touches the Server.........";
[Link]("Sending the message from Client :
"+message);
BufferedWriter dataout=new BufferedWriter(new
OutputStreamWriter([Link]()));
[Link](message,0,[Link]());
[Link]();
[Link]();
}
}

3. OUTPUT :-

Server Starting.........

Sending the message from Client : Client touches the Server.........

Incoming message to Server : Client touches the Server.........

****************************
5

ASSIGNMENT NO. 4

1. OBJECTIVE :- Write a program to send message to another computer


until ‘exit’ keyword is entered.

2. SOURCE CODE :-

//Server Program.
import [Link].*;
import [Link];
import [Link].*;
public class msgserver {
public static void main(String[] args) throws IOException {
ServerSocket s=new ServerSocket(2000);
Socket sock;
BufferedReader datain;
String str;
[Link]("Server Starting..............");
[Link]("Incoming Message from Client :");
sock=[Link]();
datain=new BufferedReader(new
InputStreamReader([Link]()));
str=[Link]();
[Link](str);
[Link]();
[Link]();
}
}

//Client program.
import [Link].*;
import [Link].*;
import [Link].*;
public class msgclient {
public static void main(String[] args) throws IOException{
Socket sock=new Socket([Link]("home-
3bb4cb7b40"),2000);
String message;
BufferedWriter dataout=new BufferedWriter(new
OutputStreamWriter([Link]()));
BufferedReader br=new BufferedReader(new
InputStreamReader([Link]));
[Link]("Start entering the message to send : ");
do {
message=[Link]();
[Link](message,0,[Link]());
[Link]();
}while(![Link]("exit"));
[Link]();
}
}
6

3. OUTPUT :-

Server Starting..............

Start entering the message to send :


Java Rules the Web!
It is the Best Programming Language.
exit

Incoming Message to Server :


Java Rules the Web! It is the Best Programming Language.

You might also like