Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Client-Socket-server!  RSS feed

 
Angela Jessi
Ranch Hand
Posts: 428
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
Can anybody explain simple application which give details how to connect client to server by socket methods? Mean how simply client can listen server sockets?
Thanks a lot in advance,
Angela
 
Carl Trusiak
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
Sun has a tutorial on this called Custom Networking it covers communications using sockets.
Hope this helps
 
maateen
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
//CLIENT PROGRAM
import java.net.*;
import java.io.*;
import java.util.*;
import java.awt.event.*;
import java.awt.*;
import java.applet.*;
public class TheClient extends Applet implements Runnable{
public int PORT=1024;
private BufferedReader read;
private PrintWriter print;
private Socket sock;
private int count=0;
private Thread listener=null;
private TextArea theTextArea;
private TextField theTextField;
public final String ERROR_MSG="ERROR";
private final String SERVER_MSG_PREFIX="SERVER";
//private final String ECHO_TO_SERVER="ECHO";
private final String CLIENT_PREFIX="CLIENT";
public URL baseURL;
public URLConnection urlConn;

public void run() {
while (true) {
try{
String line = read.readLine();
if (!line.equals(null)) {
if (line.startsWith(this.SERVER_MSG_PREFIX) ) {
line=line.substring(this.SERVER_MSG_PREFIX.length());
theTextArea.append(line+"\n");
System.out.println(line);
}
}
}
catch(Exception exp){
System.out.println(exp.toString()+"THECLIENT");
}
}//END-WHILE
}
public void init(){
try{
sock=new Socket(InetAddress.getLocalHost(),this.PORT);
read=new BufferedReader(new InputStreamReader(sock.getInputStream()));
print=new PrintWriter(sock.getOutputStream(),true);

}
catch(java.net.UnknownHostException uhexp){
System.out.println(uhexp.toString()+"HOST-CLIENT");
}
catch(java.io.IOException ioexp){
System.out.println(ioexp.toString()+"IO_CLIENT");
}/*
try{
baseURL=new URL("http://"+this.getCodeBase().getHost()+":"+this.PORT+"/");
}
catch(java.net.MalformedURLException murlexp){
System.out.print(murlexp.toString());
}
try{
urlConn=baseURL.openConnection();
read=new BufferedReader(new InputStreamReader( urlConn.getInputStream()));
if (!urlConn.getDoOutput()) {
urlConn.setDoOutput(true);
}
print=new PrintWriter(urlConn.getOutputStream());
}
catch(java.io.IOException ioexp){
System.out.println(ioexp.toString()+this.ERROR+"READING");
}*/


TheClient client=new TheClient();
}
public void start(){
listener=new Thread(this);
listener.setPriority(Thread.MIN_PRIORITY);
listener.start();
}

public TheClient(){

theTextArea =new TextArea("Main Chat Room....",10,10,TextArea.SCROLLBARS_VERTICAL_ONLY);
theTextField=new TextField();
theTextField.setSize(100,25);
theTextField.requestFocus();
theTextArea.setEditable(false);
this.setLayout(new BorderLayout(2,3));
this.add(theTextField,BorderLayout.SOUTH);
this.add(theTextArea,BorderLayout.NORTH);
this.setVisible(true);
theTextField.addKeyListener(new KeyListener(){
public void keyPressed(java.awt.event.KeyEvent A) {
if (A.getKeyCode()==A.VK_ENTER) {
if (!theTextField.getText().equals("")) {
print.println(CLIENT_PREFIX+theTextField.getText());
try{
theTextArea.append(read.readLine());
}
catch(java.io.IOException ioexp){
theTextArea.append("Error in Reading your message.."+"\n");
}
theTextField.setText("");
}//innet IF

}//outer IF
}
public void keyReleased(java.awt.event.KeyEvent A) {
}
public void keyTyped(java.awt.event.KeyEvent A) {
}
});

}
}
 
maateen
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
//SERVER PROGRAM
import java.net.*;
import java.io.*;
import java.util.*;
public class SimpleServer {//extends Thread{
public static final int PORT = 1024;
private ServerSocket theSocket;
private Socket client;
private Session clientSession;
public final String ERROR_MSG="ERROR";
private final String SERVER_MSG_PREFIX="SERVER";
private final String ECHO_TO_SERVER="ECHO";

public SimpleServer(){
String str=null;
try{
theSocket = new ServerSocket(PORT);
}
catch(java.io.IOException ioexp){
System.out.println(ioexp.toString());
}
while(true){
try{
System.out.println("waiting for clients at Port:"+PORT);
clientSession=new Session(theSocket.accept());
clientSession.start();
}
catch(java.io.IOException ioexp){
System.out.println(ioexp.toString());
} /*
catch(java.lang.InterruptedException iexp){
System.out.print(iexp.toString()+"Interrupt");
} */
}
}
public static void main(String args[]){
SimpleServer server=new SimpleServer();
}
}
 
Carl Trusiak
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maateen:
Please rad and follow the Naming Policy It's to your benefit, we hold book give aways on forums and only those who post to that forum and follow the naming policy are entered.
Looking forward to your posts
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!