3 Replies - 7042 Views - Last Post: 29 December 2008 - 05:47 AM Rate Topic: -----

#1 ucan_aps  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 31
  • Joined: 26-December 08

Converting String to Socket....?

Posted 27 December 2008 - 03:23 AM

Hai All..
Im trying to implement Peer to Peer Chat...
In that my Server stores Client(whoever log in) details into Data Base .. It does mean Im storing client sockets into database for further use..
like below line is stored in DB..
Socket[addr=/192.168.1.85,port=2437,localport=2979


it is stored as String..
then i want to get that string from DB by Client and have to convert String as Socket to make client to client connection..
PROBLEM:
my problem is i couldn't convert string into socket..
im getting below error when i try to convert

 inconvertible types
found   : java.lang.String
required: java.net.Socket


is it possible conversion...?
any other ways to store Sockets in somewhere by server and to retrieve back as Socket by Client ...?

Help Me..
Thanks in advance

This post has been edited by ucan_aps: 27 December 2008 - 03:24 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Converting String to Socket....?

#2 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 991
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Converting String to Socket....?

Posted 27 December 2008 - 04:00 AM

Have a read here:
http://homepages.uel...es/javaapps.htm

Either it will provide you an answer or it will give you ideas to help ask your question in a different way (with more information content) that will help us help you.
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Converting String to Socket....?

Posted 28 December 2008 - 10:24 PM

We told it many times... you must come a C++ background
Java does not like assigments in parameters so do it before calling your method
Was This Post Helpful? 0
  • +
  • -

#4 fsloke  Icon User is offline

  • D.I.C Regular

Reputation: 26
  • View blog
  • Posts: 413
  • Joined: 19-December 07

Re: Converting String to Socket....?

Posted 29 December 2008 - 05:47 AM

Here some sample... TCPClient.java and TCPServer.java

It look some how you need to follow the Network Diagram what what ISO 6 layering

1. Open a socket.
2. Open an input stream and output stream to the socket.
3. Read from and write to the stream according to the server's protocol.
4. Close the streams.
5. Close the socket.

//TCPClient.java


import java.net.*;
import java.io.*;
import java.util.Scanner;

public class TCPClient
{
public static void main(String [] args) throws Exception
{
Scanner inFromUser = new Scanner(System.in);
Socket serverSocket = null;
serverSocket = new Socket("10.100.3.13",1234);

String data = "";
DataInputStream input = new DataInputStream(serverSocket.getInputStream());
DataOutputStream output = new DataOutputStream(serverSocket.getOutputStream());
while(true)
{

System.out.println("Enter sentense:");
data = inFromUser.nextLine();
output.writeUTF(data);

data = input.readUTF();

System.out.println(data);
}

}
}

-------------------------------------------------------------------------------------

//TCPServer.java


import java.net.*;
import java.io.*;

public class TCPServer
{
public static void main(String [] args) throws Exception
{
ServerSocket listenClient = new ServerSocket(1234);
System.out.println("Server is listning.. ..");
Socket clientSocket = listenClient.accept();
while(true)
{

DataInputStream input = new DataInputStream(clientSocket.getInputStream());
DataOutputStream output = new DataOutputStream(clientSocket.getOutputStream());

String data = input.readUTF();

if(data.substring(0,1).compareTo("m")==0)
{

System.out.println("Message:" + data);
output.writeUTF(data.substring(1));
}

if(data.substring(0,1).compareTo("g")==0)
{
System.out.println("receive greeting from " + clientSocket.getInetAddress());
data = "Good Day";
output.writeUTF(data);
}


// System.out.println("Received from : " + + " : " + clientSocket.getPort() + "message - " + data);

// output.writeUTF(data);
}
}
}
 


Hope this will help...

Refer this for better understanding
http://java.sun.com/...definition.html

-fsloke

This post has been edited by fsloke: 29 December 2008 - 06:18 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1