Created
June 28, 2011 11:25
-
-
Save zoranzaric/1050937 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.ByteArrayOutputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.OutputStream; | |
import java.net.Socket; | |
import java.net.UnknownHostException; | |
public class Client { | |
public static void main(String[] args) throws UnknownHostException, | |
IOException { | |
Socket server = new Socket("localhost", 8888); | |
InputStream in = server.getInputStream(); | |
OutputStream out = server.getOutputStream(); | |
String outString = "Hello\n"; | |
out.write(outString.getBytes()); | |
out.close(); | |
ByteArrayOutputStream bos = new ByteArrayOutputStream(); | |
int temp; | |
while ((temp = in.read()) != -1) { | |
bos.write(temp); | |
} | |
byte[] ba = bos.toByteArray(); | |
in.close(); | |
System.out.println(new String(ba)); | |
server.close(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Exception in thread "main" java.net.SocketException: Socket closed | |
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:99) | |
at java.net.SocketOutputStream.write(SocketOutputStream.java:124) | |
at Server.main(Server.java:33) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.ByteArrayOutputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.OutputStream; | |
import java.net.ServerSocket; | |
import java.net.Socket; | |
public class Server { | |
public static void main(String[] args) throws IOException { | |
ServerSocket serverSocket = new ServerSocket(8888); | |
while (true) { | |
Socket client = serverSocket.accept(); | |
InputStream in = client.getInputStream(); | |
OutputStream out = client.getOutputStream(); | |
ByteArrayOutputStream bos = new ByteArrayOutputStream(); | |
int temp; | |
while ((temp = in.read()) != -1) { | |
bos.write(temp); | |
} | |
byte[] ba = bos.toByteArray(); | |
// close the in | |
in.close(); | |
String inString = new String(ba); | |
System.out.println(inString); | |
String outString = "World\n"; | |
out.write(outString.getBytes()); | |
out.close(); | |
client.close(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment