Skip to content

Instantly share code, notes, and snippets.

@shamun
Created October 4, 2011 22:23
Show Gist options
  • Select an option

  • Save shamun/1263022 to your computer and use it in GitHub Desktop.

Select an option

Save shamun/1263022 to your computer and use it in GitHub Desktop.
package socket;
import java.io.*;
import java.net.Socket;
import java.net.ServerSocket;
public class TCPWeb
{
private int serverPort = 0;
private ServerSocket serverSock = null;
private Socket sock = null;
private final InputStream sockInput= null;
private final OutputStream sockOutput= null;
public TCPWeb(int serverPort) throws IOException
{
this.serverPort = serverPort;
serverSock = new ServerSocket(serverPort);
System.out.println("Server started");
}
public void waitForConnections()
{
while (true)
{
try {
sock = serverSock.accept();
System.err.println("TCP Web Accepted new socket");
byte[] buf=new byte[1024];
int bytes_read = 0;
OutputStream out = new FileOutputStream("/var/www/html/video/capture.ogg");
//out.write(null,0, 0);
InputStream in = sock.getInputStream();
while ((bytes_read = in.read(buf)) != -1)
{
out.write(buf, 0, bytes_read);
System.exit(0);
}
out.close();
}
catch (IOException e){
e.printStackTrace(System.err);
}
}
}
public static void main(String argv[])
{
int port = 8000;
TCPWeb server = null;
try {
server = new TCPWeb(port);
}
catch (IOException e){
e.printStackTrace(System.err);
}
server.waitForConnections();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment