Network Program
import java.net.*;
class dataclient
{
public static DatagramSocket ds;
public static byte buffer[] = new byte[1024];
public static void main(String args[]) throws Exception
{
ds = new DatagramSocket(1999);
while(true)
{
DatagramPacket p = new DatagramPacket(buffer,buffer.length);
ds.receive(p);
System.out.println(new String(p.getData(),0,p.getLength()));
}
}
}
import java.net.*;
class dataserver
{
public static DatagramSocket ds;
public static byte buffer[] = new byte[1024];
public static void main(String args[]) throws Exception
{
InetAddress ia = InetAddress.getByName(args[0]);
ds = new DatagramSocket(1666);
int pos=0;
while(true)
{
int c = System.in.read();
switch(c)
{
case 'q':
System.out.println("Server quits");
return;
case '\r':
break;
case '\n':
ds.send(new DatagramPacket(buffer,pos,ia,1999));
pos=0;
break;
default:
buffer[pos++] = (byte) c;
}
}
}
}
// a simple client that sends lines to server and reads lines from server
import java.net.*;
import java.io.* ;
public class tcpclient
{
public static void main(String arg[]) throws IOException
{
InetAddress addr;
if(arg.length > 0)
addr = InetAddress.getByName(arg[0]);
else
addr = InetAddress.getByName("localhost");
System.out.println("addr = " + addr);
Socket s = new Socket(addr,1666);
try
{
System.out.println("Socket = " + s);
BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(s.getOutputStream())),true);
for(int i =0;i<5;i++)
{
out.println("howdy " + i);
String str = in.readLine();
System.out.println(str);
}
out.println("End");
} finally
{
System.out.println("closing...");
s.close();
}
}
}
import java.io.* ;
import java.net.* ;
public class tcpserver
{
public static void main(String arg[]) throws IOException
{
ServerSocket ss = new ServerSocket(1666);
System.out.println("Server started " + ss);
try
{
Socket s = ss.accept();
try
{
System.out.println("Connection accepted " + s);
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
// output is flushed by printwriter
PrintWriter pw = new PrintWriter(new OutputStreamWriter(s.getOutputStream()),true);
while(true)
{
String str = br.readLine();
if(str.equals("End"))
break;
System.out.println("Echoing : " + str);
pw.println(str);
}
}
finally
{
System.out.println("closing ...");
s.close();
}
}
finally
{
ss.close();
}
}
}
import java.net.*;
class client
{
public static DatagramSocket ds;
public static byte buffer[] = new byte[1024];
public static void main(String arg[]) throws Exception
{
ds = new DatagramSocket(6);
while(true)
{
String a;
DatagramPacket p = new DatagramPacket(buffer,buffer.length);
ds.receive(p);
a=new String(p.getData(),p.getLength(),1);
if (a.equals("q"))
{
System.out.println("Server response is shut off now");
return;
}
System.out.println(new String(p.getData(),0,p.getLength()));
}
}
}
import java.net.*;
class dataclient
{
public static DatagramSocket ds;
public static byte buffer[] = new byte[1024];
public static void main(String args[]) throws Exception
{
ds = new DatagramSocket(1999);
while(true)
{
DatagramPacket p = new DatagramPacket(buffer,buffer.length);
ds.receive(p);
System.out.println(new String(p.getData(),0,p.getLength()));
}
}
}
import java.net.*;
class dataserver
{
public static int serverport = 1666;
public static int clientport = 1999;
public static DatagramSocket ds;
public static byte buffer[] = new byte[1024];
public static void server() throws Exception
{
int pos=0;
while(true)
{
int c = System.in.read();
switch(c)
{
case -1:
System.out.println("Server quits");
return;
case '\r':
break;
case '\n':
ds.send(new DatagramPacket(buffer,pos,InetAddress.getLocalHost(),clientport));
pos=0;
break;
default:
buffer[pos++] = (byte) c;
}
}
}
public static void client() throws Exception
{
while(true)
{
DatagramPacket p = new DatagramPacket(buffer,buffer.length);
ds.receive(p);
System.out.println(new String(p.getData(),0,p.getLength()));
}
}
public static void main(String args[]) throws Exception
{
if(args.length ==1)
{
ds = new DatagramSocket(serverport);
server();
}
else
{
ds = new DatagramSocket(clientport);
client();
}
}
}
import java.net.*;
class inet
{
public static void main(String arg[]) throws UnknownHostException,MalformedURLException
{
InetAddress b = InetAddress.getLocalHost();
System.out.println(b);
String s = b.getHostAddress();
System.out.println(s);
InetAddress c = InetAddress.getByName(arg[0]);
System.out.println(c);
URL u = new URL("file:/j:/j002");
}
}
import java.net.*;
class server
{
public static DatagramSocket ds;
public static byte buffer[] = new byte[1024];
public static void main(String a[]) throws Exception
{
InetAddress ia = InetAddress.getByName("tmproj13");
System.out.print("Connect to ");
System.out.println(ia);
ds = new DatagramSocket(123);
int pos=0;
while(true)
{
int c = System.in.read();
switch(c)
{
case 'q':
System.out.println("Server quits");
return;
case '\r':
break;
case '\n':
DatagramPacket dp = new DatagramPacket(buffer,pos,ia,456);
ds.send(dp);
pos=0;
break;
default:
buffer[pos++] = (byte) c;
}
}
}
}
// a simple client that sends lines to server and reads lines from server
import java.net.*;
import java.io.* ;
public class tcpclient
{
public static void main(String arg[]) throws IOException
{
InetAddress addr = InetAddress.getByName(null);
// instead of null you can use localhost
System.out.println("addr = " + addr);
Socket s = new Socket(addr,tcpserver.port);
try
{
System.out.println("Socket = " + s);
BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(s.getOutputStream())),true);
for(int i =0;i<10;i++)
{
out.println("howdy " + i);
String str = in.readLine();
System.out.println(str);
}
out.println("End");
} finally{
System.out.println("closing...");
s.close();
}
}
}
import java.io.* ;
import java.net.* ;
public class tcpserver
{
public static final int port=8080;
public static void main(String arg[]) throws IOException
{
ServerSocket ss = new ServerSocket(port);
Socket s ;
System.out.println("Server started " + ss);
try {
s = ss.accept();
System.out.println("Connection accepted " + s);
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
// output is flushed by printwriter
PrintWriter pw = new PrintWriter(new OutputStreamWriter(s.getOutputStream()),true);
while(true)
{
String str = br.readLine();
if(str.equals("stop"))
break;
System.out.println("Echoing : " + str);
pw.println(str);
}
}finally {
System.out.println("closing ...");
s.close();
ss.close();
}}}
import java.net.*;
class udpclient
{
public static int serverport = 666;
public static int clientport = 999;
public static DatagramSocket ds;
public static byte buffer[] = new byte[1024];
public static void client() throws Exception
{
while(true)
{
DatagramPacket p = new DatagramPacket(buffer,buffer.length);
ds.receive(p);
System.out.print(new String(p.getData(),0,p.getLength()));
}
}
public static void main(String args[]) throws Exception
{
ds = new DatagramSocket(clientport);
client();
}
}
import java.net.*;
class udpserver
{
public static int serverport = 666;
public static int clientport = 999;
public static DatagramSocket ds;
public static byte buffer[] = new byte[1024];
public static void server() throws Exception
{
int pos=0;
while(true)
{
InetAddress ia = InetAddress.getByName("rad-tm-17");
int c = System.in.read();
ds.send(new DatagramPacket(buffer,pos,ia,clientport));
pos=0;
buffer[pos++] = (byte) c;
}
}
public static void main(String args[]) throws Exception
{
ds = new DatagramSocket(serverport);
server();
}}
Sunday, January 6, 2008
Java Example Event Programming
Posted by
Real Time Questions
at
11:52 PM
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment