==== connect ========
import java.io.*;
import java.net.*;
public class connect {
public static void main (String args[]) {
int port=7777;
if (args.length>1)
{
try {
port = (new Integer(args[1])).intValue();
} catch (Exception e) {}
}
String host="localhost";
if(args.length>0) host = args[0];
System.out.println("Use: connect [host [port]]\nYour choice: "+host+"
"+port);
try {
Socket socket = new Socket(host, port);
socket.setSoTimeout(4000); // wait up to 4 secs for a response
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
out.println("Hello to "+host);
BufferedReader in
= new BufferedReader(new InputStreamReader(System.in));
String w=in.readLine();
out.println(w);
w=in.readLine();
out.println(w);
in.close();
out.close();
} catch (Exception e) {System.out.println("connect:"+e);}
}
}
==================
=======server=======
import java.io.*;
import java.net.*;
import java.util.*;
public class server {
class inner extends Thread {
Socket s;
public void run ()
{
print(s);
}
}
ServerSocket socket = null;
boolean fs;
String Filter="";
public static void main (String args[]) {
String filter="";
int port=7777;
boolean fileStore=false;
if (args.length>0) {filter=args[0];}
if (args.length>1)
{
try {
port = (new Integer(args[1])).intValue();
} catch (Exception e) {}
}
if (args.length>2) fileStore=true;
System.out.println("Multithread.\nUse: server [filter [port [store]]]\nYour
choice: |"+filter+"| "+port+" "+fileStore);
server s=new server(filter,port,fileStore);
while (true)
try {
Socket next_socket = s.socket.accept();
next_socket.setSoTimeout(1000*60*60);
inner next_inner = s.new inner();
next_inner.s=next_socket;
next_inner.start();
} catch (Exception e) {System.out.println("Servere main cikl:"+e);}
}
public void print (Socket s)
{
System.out.println(this.toString()+" stars");
PrintWriter out=null;
String str=null,old_str=null;
try {
// s = socket.accept();
Calendar rightNow = Calendar.getInstance();
int time=rightNow.get(Calendar.MONTH)*100000000+
rightNow.get(Calendar.DAY_OF_MONTH)*1000000+
rightNow.get(Calendar.HOUR_OF_DAY)*10000+
rightNow.get(Calendar.MINUTE)*100+
rightNow.get(Calendar.SECOND);
if (fs) out = new PrintWriter(new BufferedWriter(new
FileWriter("S"+time)));
BufferedReader in
= new BufferedReader(new InputStreamReader(s.getInputStream()));
while (true)
{
str=in.readLine();
if (str==null) break;
old_str=str;
if (Filter.length()>0&&!old_str.startsWith(Filter)) continue;
System.out.println(str);
if (fs) { out.println(str); out.flush(); }
}
}
catch (Exception e) { System.out.println("Server:"+e); }
finally { if(fs&&out!=null) out.close(); out=null; }
if (Filter.length()==0||old_str==null||old_str.startsWith(Filter)
)System.out.println(this.toString()+" End connection\n");
}
public server (String filter, int port, boolean store)
{
Filter=filter;
fs=store;
try {
socket=new ServerSocket(port,5);
socket.setSoTimeout(1000*60*60);
}
catch (Exception e) {System.out.println("Server constructor:"+e); }
}
}
==================
or so.
Alex Kizub.