Connect with a Swift client to a Java server using sockets - java

How do I connect a Swift client to a Java server using sockets?
How can I send different data types between the client and the server?
ServerSocket serverSocket = new ServerSocket(12345);
while(true) {
Socket socket = serverSocket.accept();
System.out.println(new BufferedReader(new InputStreamReader(socket.getInputStream())).readLine());
}

You can use the SocketRocket ObjC framework bridged to Swift
https://github.com/square/SocketRocket
Then you just create an instance of the class and use it in Swift, i.e.:
var socket = SRWebSocket(...)
socket.open()
socket.send()
socket.close()
Example:
You create a NSURLRequest:
var urlRequest = NSURLRequest(URL: NSURL(string: "http://host.com"))
then init the socket with it
socket = SRWebSocket(URLRequest: urlRequest)
No need to link the framework, just import the files

Related

Java - Program works only for localhost

I use the following code for the client
InetAddress host = InetAddress.getByName("XXX.XXX.XXX.XXX");
Socket socket = new Socket(host,serverPort);
and for the server
ServerSocket serverSocket = new ServerSocket(portAdd);
Socket server = serverSocket.accept();
It works if I am using localhost but not when I use an IP address.
Firewall is not blocking the connection.

Is it possible to transfer (a String, for example) from a TCP Server to a UDP Client?

I'm working on TCP and UDP just to learn the basics of how networks and protocols work in Java, and the task my professor has set me is to have the following:
A TCP Client that connects to a TCP Server, which communicates with a UDP Client that connects to a UDP Server, which gets/alters data from a Database.
I have everything set up and working, but right now the TCP Server calls the UDP Client as an object, and passes its information through a method call, rather than over the internet.
For TCP I'm using server sockets and input stream / buffered stream readers, and for the UDP I'm using datagram packets. So I'm not really sure how I can send from one to the other directly, or if it's even possible.
Thanks in advance for any help/advice/guidance!
Jona
**EDIT:
What I mean by passing through a method call:
//In TCPServer class
UDPClient udpc = new UDPClient();
String result = udpc.GetFromUDPServer(clientCommand);
//In UDPClient class
public class UDPClient{
public String GetFromUDPServer(String clientCommand){
try{
//Create a datagram socket
DatagramSocket sock = new DatagramSocket();
byte[] buffer = clientCommand.getBytes("UTF-8");
InetAddress ipAddress = InetAddress.getByName("localhost");
DatagramPacket packet = new DatagramPacket(buffer, buffer.length, ipAddress, 447);
...

ServerSocket for datagram type socket?

I have a ftp client-server program which currently uses TCP. I need to now make it work using UDP, so using datagram sockets instead.
They way it was working was to create a ServerSocket which would listen for a connection, then give that connection to a Socket, and start a separate thread for that socket.
But now using DatagramSocket, there's no equivalent DatagramServerSocket, so I've no idea how I can give clients separate sockets anymore. Is there a server socket for datagrams?
import java.io.IOException;
import java.net.*;
public class Server {
public static void main(String[] args) throws IOException {
Socket sock;
ServerSocket servSock = new ServerSocket(4444, 600);
while(true){
sock = servSock.accept();
new ClientHandler(sock).start();
}
}
}
You don't need to give clients separate sockets. You have the source address in each datagram. That tells you which client the datagram is from. You only need one socket in a UDP server.
Well, UDP is a connectionless protocol. Doing decent file transfer would probably mean implementing a decent chunk of TCP-like functionality.
DatagramSocket serverSocket = new DatagramSocket(PORT_NUMBER);`
Will act as a server socket.
alternatively you could use
DatagramChannel channel = DatagramChannel.open();
channel.socket().bind(new InetSocketAddress(9999));

Read data from a particular port and specified IP using java

I would like to read the messages from a particular port.For example the IP is 1.2.3.4 and the port is 1000. Already the IP is used for receiving some messages. What I would like to do is to listen to that particular IP and receive all the messages using a java program. Will
SocketServer do the purpose??
ServerSocket ss = new ServerSocket(1000);
Socket socket = new Socket("1.2.3.4",1000);
socket = ss.accept();
Is it possible to read every contents that are being received by the particular IP and port?
To listen to a specific address you have to create a ServerSocket like this
ServerSocket ss = new ServerSocket(); // Unbound socket
ss.bind(new InetSocketAddress("1.2.3.4", 1000)); // Bind the socket to a specific interface
Socket client = ss.accept();
This way the server socket is bound to a specific network interface and will only receive incoming connections from it.

Communiation with multiple clients via Sockets

I have a Server Socket and 3-4 android devices as clients. I'm using TCP/IP for communications. Which is the best method. Should I use multiple ports for each client? Or should I use same port. If using same function then how should I identify the communication addressed to different devices?
No, you do not need several ports.
ServerSocket server = new ServerSocket(port);
while (true)
{
Socket socket = server.accept();
// do something with this socket - aka 1 client
new SomeClientClass(socket);
InputStream in = socket.getInputStream();
in.read(byte[]);
OutputStream out = socket.getOutputStream;
// out will only write response to its own client.
// when this new SomeClientClassis created, method returns to this point
// in while loop and waits for the next client
}
You can use one port. The client can send you its id. If it can't you can look at the clients IP address to workout which one it is.
There are thousands of TCP client/server code examples on the web, but I would start with the sample code which comes with the JDK,

Categories

Resources