Click here to Skip to main content
14,739,281 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi guys,
i really need a java code for client server communications over tcp with a crc check on both ends.

thanx in advance
Posted
Comments
DaveAuld 2-Dec-11 11:29am
   
It doesn't quite work like that on CP. Nobody just hands over code. What have you got so far, what are you struggling with?

1 solution

You can get this code by writing it. Nobody knows your requirements for application-level protocol and other requirements, so there is no such thing as ready-to-use code.

If you do this work, face any problems and are able to describe them accurately, we will gladly help you.

Good luck,
—SA
   
Comments
thazilla 2-Dec-11 11:37am
   
import java.awt.Color;
import java.awt.BorderLayout;
import java.awt.event.*;
import javax.swing.*;

import java.io.*;
import java.net.*;

class SocketServer extends JFrame
implements ActionListener {

JButton button;
JLabel label = new JLabel("Text received over socket:");
JPanel panel;
JTextArea textArea = new JTextArea();
ServerSocket server = null;
Socket client = null;
BufferedReader inn = null;
PrintWriter outt = null;
String message;

SocketServer(){ //Begin Constructor
button = new JButton("Click Me");
button.addActionListener(this);

panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.setBackground(Color.white);
getContentPane().add(panel);
panel.add("North", label);
panel.add("Center", textArea);
panel.add("South", button);

} //End Constructor

public void actionPerformed(ActionEvent event) {
Object source = event.getSource();

if(source == button){
textArea.setText(message);
}
}

public void listenSocket(){

try{
server = new ServerSocket(4444);
} catch (IOException e) {
System.out.println("Could not listen on port 4444");
System.exit(-1);
}

try{
client = server.accept();
} catch (IOException e) {
System.out.println("Accept failed: 4444");
System.exit(-1);
}

try{
inn = new BufferedReader(new InputStreamReader(client.getInputStream()));
// outt = new PrintWriter(client.getOutputStream(), true);
// DataOutputStream outt = new DataOutputStream(client.getOutputStream());
//outt.writeBytes();
} catch (IOException e) {
System.out.println("Accept failed: 4444");
System.exit(-1);
}

while(true){
try{
message = inn.readLine();
//Send data back to client

String reversedmessage= new StringBuffer(message).reverse().toString();
DataOutputStream outt = new DataOutputStream(client.getOutputStream());
outt.writeBytes(reversedmessage + "\n");
} catch (IOException e) {
System.out.println("Read failed");
System.exit(-1);
}
}
}

protected void finalize(){
//Clean up
try{
inn.close();
outt.close();
server.close();
} catch (IOException e) {
System.out.println("Could not close.");
System.exit(-1);
}
}

public static void main(String[] args){
SocketServer frame = new SocketServer();
frame.setTitle("Server Program");
WindowListener l = new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
};
frame.addWindowListener(l);
frame.pack();
frame.setVisible(true);
frame.listenSocket();
}
}

this is what i have for my server
thazilla 2-Dec-11 11:43am
   
import java.awt.Color;
import java.awt.BorderLayout;
import java.awt.event.*;
import javax.swing.*;

import java.io.*;
import java.net.*;

import sun.misc.CRC16;

import java.io.UnsupportedEncodingException;

import static java.lang.System.out;

class SocketClient extends javax.swing.JFrame
implements ActionListener {

JLabel text, clicked;
JButton button;
JPanel panel;
JTextField textField;
Socket socket = null;
PrintWriter out = null;
BufferedReader in = null;

SocketClient(){ //Begin Constructor
text = new JLabel("Text to send over socket:");
textField = new JTextField(20);
button = new JButton("Click Me");
button.addActionListener(this);

panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.setBackground(Color.blue);
getContentPane().add(panel);
panel.add("North", text);
panel.add("Center", textField);
panel.add("South", button);

} //End Constructor



public void actionPerformed(ActionEvent event){
Object source = event.getSource();

if(source == button){

//Send data over socket
String text = textField.getText();
String reverse= new StringBuffer(text).reverse().toString();
out.println(text);
textField.setText(new String(""));
//Receive text from server
try{
String line = in.readLine();
System.out.println("Text received :" + line);
} catch (IOException e){
System.out.println("Read failed");
System.exit(1);
}
}

}

public void listenSocket(){
//Create socket connection
try{
socket = new Socket("localhost", 4444);
out = new PrintWriter(socket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
} catch (UnknownHostException e) {
System.out.println("Unknown host: kq6py.eng");
System.exit(1);
} catch (IOException e) {
System.out.println("No I/O");
System.exit(1);
}
}



public static void main(String[] args){
SocketClient frame = new SocketClient();
frame.setTitle("Client Program");
WindowListener l = new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
};

frame.addWindowListener(l);
frame.pack();
frame.setVisible(true);
frame.listenSocket();
}

}

and this is my client code
it wont run with out the server running but ive been struggling to put a
crc check on both ends, the client crc will attch its value on to the msg to be sent and the server will recieve the msg run its own crc validate and send an acknowlege but that optional

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900