Skip to content
Snippets Groups Projects
Commit 92ba385a authored by Christopher Bohn's avatar Christopher Bohn :thinking:
Browse files

Initial commit

parents
No related branches found
No related tags found
No related merge requests found
# Project-specific
doc/
# Mac file finder metadata
.DS_Store
# MS Office temporary file
~*
# Emacs backup file
*~
# LaTeX files
*.aux
*.log
*.out
*.tex.gz
*.synctex.gz
*.texmk
*.dvi
*.fls
*.fdb_latexmk
# C files
*.o
*.out
# Java files
*.class
# Python files
*.pyc
*.pyo
# JetBrains (IntelliJ IDEA, PyCharm, etc) files
.idea/
cmake-build-*/
out/
bin/
*.iml
*.iws
*.ipr
# Eclipse files
bin/
.settings/
.classpath
.project
# Maven
target/
# Miscellaneous
tmp/
*.tmp
*.bak
*.swp
# Socket Chat
Socket Chat is a simple 2-way chat program, essentially a bi-directional echo server/client. It is remarkably
non-robust, as its sole purpose is to demonstrate the use of Java sockets.
\ No newline at end of file
pom.xml 0 → 100644
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>edu.unl.cse</groupId>
<artifactId>SocketChat</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>edu.unl.cse.socketchat.Chat</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
/*
* Copyright (c) 2019 Christopher A. Bohn, bohn@unl.edu.
*/
package edu.unl.cse.socketchat;
import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
public class Chat {
private Socket socket;
private Scanner scanner;
private boolean isHost;
private PrintStream[] output = new PrintStream[2];
private BufferedReader[] input = new BufferedReader[2];
public Chat() {
scanner = new Scanner(System.in);
try {
socket = connect();
if (isHost) {
input[0] = new BufferedReader(new InputStreamReader(System.in));
input[1] = new BufferedReader(new InputStreamReader(socket.getInputStream()));
output[0] = new PrintStream(socket.getOutputStream());
output[1] = System.out;
} else {
input[0] = new BufferedReader(new InputStreamReader(socket.getInputStream()));
input[1] = new BufferedReader(new InputStreamReader(System.in));
output[0] = System.out;
output[1] = new PrintStream(socket.getOutputStream());
}
} catch (IOException ioException) {
System.err.println("Connection failed: " + ioException);
System.exit(1);
}
}
public void communicate() {
System.out.println("Host goes first.");
String message = "";
int turn = 0;
try {
while (!message.equals("EXIT")) {
message = input[turn].readLine();
output[turn].println(message);
turn = (turn + 1) % 2;
}
socket.close();
} catch (IOException ioException) {
System.err.println("Connection dropped: " + ioException);
System.exit(1);
}
}
private Socket connect() throws IOException {
System.out.print("Are you the chat host? [Y] ");
String answerString = scanner.nextLine().toUpperCase();
char answer = answerString.length() > 0 ? answerString.charAt(0) : 'Y';
isHost = (answer != 'N');
return isHost ? connectAsServer(6858) : connectAsClient(6858);
}
private Socket connectAsServer(int port) throws IOException {
byte[] address = InetAddress.getLocalHost().getAddress();
// byte[] address = InetAddress.getLoopbackAddress().getAddress();
System.out.println("Host address: " + address[0] + "." + address[1] + "." + address[2] + "." + address[3]);
ServerSocket serverSocket = new ServerSocket(port);
return serverSocket.accept();
}
private Socket connectAsClient(int port) throws IOException {
System.out.print("Enter IP address of host <##.##.##.##>: ");
String addressString = scanner.nextLine();
String tokens[] = addressString.split("\\.");
byte[] address = new byte[4];
for (int i = 0; i < 4; i++) {
address[i] = Byte.valueOf(tokens[i]);
}
return new Socket(InetAddress.getByAddress(address), port);
}
public static void main(String args[]) {
Chat chat = new Chat();
chat.communicate();
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment