Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
Socket Chat
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
dommai1007
Socket Chat
Commits
92ba385a
Commit
92ba385a
authored
5 years ago
by
Christopher Bohn
Browse files
Options
Downloads
Patches
Plain Diff
Initial commit
parents
No related branches found
No related tags found
No related merge requests found
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
.gitignore
+56
-0
56 additions, 0 deletions
.gitignore
README.md
+5
-0
5 additions, 0 deletions
README.md
pom.xml
+45
-0
45 additions, 0 deletions
pom.xml
src/main/java/edu/unl/cse/socketchat/Chat.java
+90
-0
90 additions, 0 deletions
src/main/java/edu/unl/cse/socketchat/Chat.java
with
196 additions
and
0 deletions
.gitignore
0 → 100644
+
56
−
0
View file @
92ba385a
# 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
This diff is collapsed.
Click to expand it.
README.md
0 → 100644
+
5
−
0
View file @
92ba385a
# 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
This diff is collapsed.
Click to expand it.
pom.xml
0 → 100644
+
45
−
0
View file @
92ba385a
<?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>
This diff is collapsed.
Click to expand it.
src/main/java/edu/unl/cse/socketchat/Chat.java
0 → 100644
+
90
−
0
View file @
92ba385a
/*
* 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
();
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment