Bi-directional communication using TCP socket

K

Kathryn Bean

I have to implement bi-directional channel between n nodes, where one node
can be connected to one or more nodes using TCP sockets.
The nodes send several messages to each other. My application will be
running on Sun Unix platforms.

I think it can be implemented as a process running two threads, where one
thread is a server and another thread is a client.
I am not sure how to implement this.

Thank you in advance.
Kathryn
 
A

Alex Kizub

==== 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top